arduinoで真理値表

#概要

arduinoで真理値表やってみた。
シリアルから、打ち込んでRUNすると、キーパットを入力としてLCDに表示する。
簡単に言うと、論理回路エミュレータ。
in,out,wire,xor,and,orを実装。

#写真

![DSCN0333.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/24c380d0-523a-94dd-d412-ca897f41d4d6.jpeg)

#実行結果

![image](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/942168e8-dd46-ab1a-5d11-09aee7535ec0.png)

#サンプルコード


```
#include <LiquidCrystal.h>

#define btRIGHT				0
#define btUP				1
#define btDOWN				2
#define btLEFT				3
#define btSELECT			4
#define btNONE				5
#define err					6
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char buf[20];
char w[8];
char io[8];
char p0[8];
char p1[8];
char p2[8];
char p3[8];
int Ptr = 0;
int OP = 0;
char Val;
char RD(int ado)
{
	char v = buf[ado];
	return v;
}
void WR(int ado, char data)
{
	buf[ado] = data;
}
void putchr(char c)
{
	Serial.print(c);
}
boolean num()
{
	if (48 <= RD(Ptr) && RD(Ptr) <= 57) return true;
	else return false;
}
boolean getnm2()
{
	char ch;
	if (!num()) return false;
	int n = 0;
	do
	{
		n *= 10;
		ch = RD(Ptr++);
		n += ch - 48;
	} while (num());
	Val = n;
	return true;
}
int read(int adc_key_in)
{
	if (adc_key_in > 1000) return btNONE;
	if (adc_key_in < 50) return btRIGHT;
	if (adc_key_in < 250) return btUP;
	if (adc_key_in < 450) return btDOWN;
	if (adc_key_in < 650) return btLEFT;
	if (adc_key_in < 850) return btSELECT;
	return err;
}
void volt(int adc_key_in)
{
	lcd.setCursor(11, 1);
	lcd.print((float) adc_key_in * 5 / 1024);
}
void state(int lcd_key)
{
	lcd.setCursor(0, 1);
	switch (lcd_key)
	{
	case btRIGHT:
		Serial.println("RIGHT ");
	break;
	case btLEFT:
		Serial.println("LEFT  ");
		w[1] = 1;
	break;
	case btUP:
		Serial.println("UP    ");
	break;
	case btDOWN:
		Serial.println("DOWN  ");
	break;
	case btSELECT:
		Serial.println("SELECT");
		w[0] = 1;
	break;
	case btNONE:
	break;
	case err:
	break;
	}
}
void setup()
{
	Serial.begin(115200);
	Serial.println("echo");
	lcd.begin(16, 2);
	lcd.setCursor(0, 0);
}
void loop()
{
	int i;
	int lcd_key = 0;
	w[0] = 0;
	w[1] = 0;
	w[2] = 0;
	lcd_key = read(analogRead(0));
	state(lcd_key);
	while (Serial.available() > 0)
	{
		char ch = Serial.read();
		putchr(ch);
		if (ch == '\r')
		{
			WR(Ptr++, '\0');
			if (strncmp(buf, "list", 4) == 0)
			{
				Serial.println("ok");
				for (i = 0; i < OP; i++)
				{
					Serial.print(p0[i], DEC);
					Serial.print(" ");
					Serial.print(p1[i], DEC);
					Serial.print(" ");
					Serial.print(p2[i], DEC);
					Serial.print(" ");
					Serial.println(p3[i], DEC);
				}
			}
			else if (strncmp(buf, "new", 3) == 0)
			{
				Serial.println("ok");
				OP = 0;
			}
			else if (strncmp(buf, "run", 3) == 0)
			{
				Serial.println("ok");
				while(1)
				{
					for (i = 0; i < OP; i++)
					{
						if (p0[i] == 0) w[p3[i]] = w[p1[i]] ^ w[p2[i]];
						if (p0[i] == 1) w[p3[i]] = w[p1[i]] & w[p2[i]];
						if (p0[i] == 2) w[p3[i]] = w[p1[i]] | w[p2[i]];
					}
					for (i = 0; i < 8; i++)
					{
						if (io[i] == 0)
						{
							if (w[i] == 0)
							{
								lcd.setCursor(i, 0);
								lcd.print("0");
							}
							else
							{
								lcd.setCursor(i, 0);
								lcd.print("1");
							}
						}
						if (io[i] == 2)
						{
							if (w[i] == 0)
							{
								lcd.setCursor(i, 1);
								lcd.print("0");
							}
							else
							{
								lcd.setCursor(i, 1);
								lcd.print("1");
							}
						}
					}
					w[0] = 0;
					w[1] = 0;
					w[2] = 0;
					lcd_key = read(analogRead(0));
					state(lcd_key);
				}
			}
			else if (strncmp(buf, "in ", 3) == 0)
			{
				Serial.println("ok");
				Ptr = 3;
				if (getnm2())
				{
					io[Val] = 0;
				}
			}
			else if (strncmp(buf, "wire ", 5) == 0)
			{
				Serial.println("ok");
				Ptr = 5;
				if (getnm2())
				{
					io[Val] = 1;
				}
			}
			else if (strncmp(buf, "out ", 4) == 0)
			{
				Serial.println("ok");
				Ptr = 4;
				if (getnm2())
				{
					io[Val] = 2;
				}
			}
			else if (strncmp(buf, "xor ", 4) == 0)
			{
				Serial.println("ok");
				Ptr = 4;
				p0[OP] = 0;
				if (getnm2())
				{
					p1[OP] = Val;
					Ptr++;
					if (getnm2())
					{
						p2[OP] = Val;
						Ptr++;
						if (getnm2())
						{
							p3[OP] = Val;
						}
					}
				}
				OP++;
			}
			else if (strncmp(buf, "and ", 4) == 0)
			{
				Serial.println("ok");
				Ptr = 4;
				p0[OP] = 1;
				if (getnm2())
				{
					p1[OP] = Val;
					Ptr++;
					if (getnm2())
					{
						p2[OP] = Val;
						Ptr++;
						if (getnm2())
						{
							p3[OP] = Val;
						}
					}
				}
				OP++;
			}
			else if (strncmp(buf, "or ", 3) == 0)
			{
				Serial.println("ok");
				Ptr = 3;
				p0[OP] = 2;
				if (getnm2())
				{
					p1[OP] = Val;
					Ptr++;
					if (getnm2())
					{
						p2[OP] = Val;
						Ptr++;
						if (getnm2())
						{
							p3[OP] = Val;
						}
					}
				}
				OP++;
			}
			else
			{
				Serial.println("err");
			}
			Ptr = 0;
		}
		else
		{
			WR(Ptr++, ch);
		}
	}
}





```

以上。