#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//Global - OLED screen setup
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Global - Pin variables
int buttonPin = 2; // recieves a signal from pin 2 (Button)
int buttonPin2 = 4; // recieves a signal from pin 4 (Button)
int buttonPin3 = 7; // recieves a signal from pin 7 (Button)
int ledPin = 13; // recieves a signal from pin 4 (LED)
int buttonState1 = 0; // Is the button currently pressed?
int buttonState2 = 0; // Is the button currently pressed?
int buttonState3 = 0; // Is the button currently pressed?
// Logic variables
int currentOperator = 0; // 0: OR, 1: AND
char stateDisplay[] = "OR"; // Current operator
int logicGate1 = 0;
int logicGate2 = 0;
char gateDisplay1[2];
char gateDisplay2[3];
bool switchLimiter;
//Global - recieves a string and a X, Y to DRAW a TEXT on screen
void basicText(char text[], int xPos, int yPos){
display.setCursor(xPos, yPos);
display.println(text);
}
// Display information on screen
void updateDisplay (){
switch(currentOperator) {
case 0:
sprintf(stateDisplay, "OR");
break;
case 1:
sprintf(stateDisplay, "AND");
break;
case 2:
sprintf(stateDisplay, "NOR");
break;
case 3:
sprintf(stateDisplay, "NAND");
break;
}
// String setup - setup the string on display
sprintf(gateDisplay1, "%d", logicGate1);
sprintf(gateDisplay2, "%d", logicGate2);
// Text setup
display.setTextSize(1);
display.setTextColor(WHITE);
// Display Text
basicText("Logic Gate Simulator", 0, 0);
basicText("Current Operat:", 0, 9);
basicText(stateDisplay, 95, 9);
basicText("Button 1 state:", 0, 20);
basicText(gateDisplay1, 95, 20);
basicText("Button 2 state:", 0, 30);
basicText(gateDisplay2, 95, 30);
basicText("Can you light up the LED?", 0, 45);
display.display();
}
// Switch current LED state
void lightSwitch () {
// Change LED state
if(digitalRead(ledPin) == 0) {
digitalWrite(ledPin, 1);
} else {
digitalWrite(ledPin, 0);
}
delay(500);
}
// switch state
int stateSwitch(int numb) {
if(numb == 0) {
numb = 1;
} else {
numb = 0;
}
return numb;
}
void setup() {
Serial.begin(9600);
// initialize digital pin LED_BUILTIN as an output.
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(9, OUTPUT); // recieves a signal from pin 7
// initialize the OLED object
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
}
// the loop function runs over and over again forever
void loop() {
// Detects if buttons are pressed
buttonState1 = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
// Logic when button 1 or 2 is pressed
if(buttonState1 == HIGH || buttonState2 == HIGH){
// Switch states in logic gates when a button is pressed
if(buttonState1 == HIGH){
logicGate1 = stateSwitch(logicGate1);
delay(100);
}
// Switch states in logic gates when a button is pressed
if(buttonState2 == HIGH){
logicGate2 = stateSwitch(logicGate2);
delay(100);
}
// Clear the buffer.
display.clearDisplay();
updateDisplay();
// Light LED if conditions are met
if(currentOperator == 0){
// OR Operator
if(logicGate1 == 1 || logicGate2 == 1){
digitalWrite(ledPin, 1);
delay(100);
} else {
digitalWrite(ledPin, 0);
delay(100);
}
}else if(currentOperator == 1){
// AND Operator
if(logicGate1 == 1 && logicGate2 == 1){
digitalWrite(ledPin, 1);
delay(100);
} else {
digitalWrite(ledPin, 0);
delay(100);
}
}
// Logic when NO button is pressed
} else {
// Clear the buffer.
display.clearDisplay();
updateDisplay();
// Light LED if conditions are met
if(currentOperator == 2){
// NOR
if(!(logicGate1 == 1 && logicGate2 == 1)){
digitalWrite(ledPin, 1);
}else{
digitalWrite(ledPin, 0);
}
}else if(currentOperator == 3){
// NAND
if(!(logicGate1 == 1 || logicGate2 == 1)){
digitalWrite(ledPin, 1);
} else {
digitalWrite(ledPin, 0);
}
}
}
// Logic when button 3 is pressed
if(buttonState3 == HIGH){
// Cycle between availiable logic operators
if(currentOperator == 3){
currentOperator = 0;
digitalWrite(ledPin, 0);
} else if (currentOperator <= 4){
currentOperator++;
}else {
currentOperator = 0;
digitalWrite(ledPin, 0);
}
delay(250);
}
// Nescessary delay for currently unknown reasons
delay(1);
}