Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
- int xPin = A0;
- int yPin = A1;
- int buttonPin = 2;
- int xVal;
- int yVal;
- int buttonState;
- int lastButtonState = HIGH;
- // Constants for modes
- const int MODE_GRID = 0;
- const int MODE_OPERATOR = 1;
- const int MODE_RESULT = 2;
- // Current mode
- int currentMode = MODE_GRID;
- // Variables for cursor position
- int cursorX = 0;
- int cursorY = 0;
- // Grid of values (0 or 1) for each position
- int grid[2][8] = {
- {0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0}
- };
- // Backup of original values
- int gridBackup[2][8] = {
- {0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0, 0, 0, 0}
- };
- // Operator selection
- int currentOperator = 0;
- int selectedOption = 0; // 0 for REVERT, 1 for RESET
- String operators[] = {"AND", "OR ", "XOR", "NAND", "NOR ", "XNOR"};
- // State tracking
- bool isCalculated = false;
- // Debounce variables
- unsigned long lastMoveTime = 0;
- unsigned long lastButtonTime = 0;
- const int moveDelay = 200; // ms between cursor movements
- const int buttonDelay = 300; // ms between button presses
- byte charXor[8] = {
- 0b00000,
- 0b01110,
- 0b10101,
- 0b11111,
- 0b10101,
- 0b01110,
- 0b00000,
- 0b00000
- };
- byte charOr[8] = {
- B00000,
- B10001,
- B01010,
- B00100,
- B00000,
- B00000,
- B00000,
- B00000
- };
- byte charNot[8] = {
- B00000,
- B00000,
- B00000,
- B11111,
- B10000,
- B00000,
- B00000,
- B00000
- };
- void setup() {
- lcd.begin(16, 2);
- pinMode(xPin, INPUT);
- pinMode(yPin, INPUT);
- pinMode(buttonPin, INPUT_PULLUP);
- Serial.begin(9600);
- lcd.createChar(0, charOr);
- lcd.createChar(1, charXor);
- lcd.createChar(2, charNot);
- updateDisplay(); // Display initial grid and UI elements
- lcd.setCursor(cursorX, cursorY); // Set initial cursor position
- lcd.cursor(); // Show the cursor
- }
- void loop() {
- xVal = analogRead(xPin);
- yVal = analogRead(yPin);
- buttonState = digitalRead(buttonPin);
- // Handle input based on current mode
- switch (currentMode) {
- case MODE_GRID:
- handleGridMode();
- break;
- case MODE_OPERATOR:
- handleOperatorSelectMode();
- break;
- case MODE_RESULT:
- handleResultMode();
- break;
- }
- lastButtonState = buttonState;
- }
- void resetBlink() {
- lcd.cursor();
- lcd.noBlink();
- }
- void handleGridMode() {
- // Only move the cursor if enough time has passed
- if (millis() - lastMoveTime > moveDelay) {
- // Check X-axis (left/right)
- if (xVal < 100) { // Moving left
- if (cursorX == 10) {
- cursorX = 7;
- } else if (cursorX > 0) {
- cursorX--;
- }
- lastMoveTime = millis();
- lcd.setCursor(cursorX, cursorY);
- } else if (xVal > 924) { // Moving right
- if (cursorX == 7) {
- cursorX = 10; // Jump from 7 to 10
- } else if (cursorX < 7) {
- cursorX++;
- }
- lastMoveTime = millis();
- lcd.setCursor(cursorX, cursorY);
- }
- // Check Y-axis (up/down)
- if (yVal < 100 && cursorY == 0 && !isCalculated) {
- cursorY++;
- lastMoveTime = millis();
- lcd.setCursor(cursorX, cursorY);
- } else if (yVal > 924 && cursorY == 1) {
- cursorY--;
- lastMoveTime = millis();
- lcd.setCursor(cursorX, cursorY);
- }
- if (cursorX == 10 && cursorY == 1) {
- lcd.noCursor();
- lcd.blink();
- } else {
- lcd.cursor();
- lcd.noBlink();
- }
- }
- // Check button press with debounce
- if (buttonState == LOW && lastButtonState == HIGH &&
- (millis() - lastButtonTime > buttonDelay)) {
- // If at position (10,0), switch to operator mode
- if (cursorX == 10 && cursorY == 0) {
- currentMode = MODE_OPERATOR;
- updateDisplay(); // renders selection arrows
- lcd.noCursor();
- lcd.blink();
- } else if (cursorX == 10 && cursorY == 1) {
- executeOperation();
- } else if (cursorX < 8) {
- // Toggle the value at current position
- grid[cursorY][cursorX] = 1 - grid[cursorY][cursorX]; // Toggle between 0 and 1
- updateDisplay();
- lcd.setCursor(cursorX, cursorY);
- }
- lastButtonTime = millis();
- }
- }
- void handleOperatorSelectMode() {
- if (millis() - lastMoveTime > moveDelay) {
- // Move left/right to change operator
- if (xVal < 100 && currentOperator > 0) {
- currentOperator--;
- lastMoveTime = millis();
- updateDisplay();
- lcd.setCursor(10, 0); // Center of operator display
- lcd.blink();
- } else if (xVal > 924 && currentOperator < 5) {
- currentOperator++;
- lastMoveTime = millis();
- updateDisplay();
- lcd.setCursor(10, 0); // Center of operator display
- lcd.blink();
- }
- }
- // Return to grid mode when button is pressed
- if (buttonState == LOW && lastButtonState == HIGH &&
- (millis() - lastButtonTime > buttonDelay)) {
- currentMode = MODE_GRID;
- cursorX = 10;
- cursorY = 0;
- resetBlink();
- lcd.setCursor(cursorX, cursorY);
- lastButtonTime = millis();
- updateDisplay(); // removes selection arrows
- }
- }
- void executeOperation() {
- // Move between REVERT and RESET buttons
- for (int y = 0; y < 2; y++) {
- for (int x = 0; x < 8; x++) {
- gridBackup[y][x] = grid[y][x];
- }
- }
- for (int i = 0; i < 8; i++) {
- switch (currentOperator) {
- case 0: // AND
- grid[1][i] = grid[0][i] & grid[1][i];
- break;
- case 1: // OR
- grid[1][i] = grid[0][i] | grid[1][i];
- break;
- case 2: // XOR
- grid[1][i] = grid[0][i] ^ grid[1][i];
- break;
- case 3: // NAND
- grid[1][i] = !(grid[0][i] & grid[1][i]);
- break;
- case 4: // NOR
- grid[1][i] = !(grid[0][i] | grid[1][i]);
- break;
- case 5: // XNOR
- grid[1][i] = !(grid[0][i] ^ grid[1][i]);
- break;
- }
- }
- isCalculated = true;
- currentMode = MODE_RESULT;
- updateDisplay();
- lcd.setCursor(3, 0); // Position on REVERT button
- lcd.blink();
- }
- void handleResultMode() {
- // Handle navigation between options if enough time has passed
- if (millis() - lastMoveTime > moveDelay) {
- if (xVal < 100) { // Move left to REVERT
- selectedOption = 0;
- lcd.setCursor(3, 0);
- lastMoveTime = millis();
- } else if (xVal > 924) { // Move right to RESET
- selectedOption = 1;
- lcd.setCursor(12, 0);
- lastMoveTime = millis();
- }
- }
- // Handle button press with debounce
- if (buttonState == LOW && lastButtonState == HIGH &&
- (millis() - lastButtonTime > buttonDelay)) {
- if (selectedOption == 0) {
- // REVERT - Restore from backup
- for (int y = 0; y < 2; y++) {
- for (int x = 0; x < 8; x++) {
- grid[y][x] = gridBackup[y][x];
- }
- }
- } else {
- // RESET - Set all to zero
- for (int y = 0; y < 2; y++) {
- for (int x = 0; x < 8; x++) {
- grid[y][x] = 0;
- }
- }
- }
- isCalculated = false;
- currentMode = MODE_GRID;
- cursorX = 0;
- cursorY = 0;
- updateDisplay();
- lcd.setCursor(cursorX, cursorY);
- resetBlink();
- lastButtonTime = millis();
- }
- }
- void updateDisplay() {
- // Clear display
- lcd.clear();
- if (!isCalculated) {
- // Normal mode - show both rows and operator
- // Update first row - bits
- lcd.setCursor(0, 0);
- for (int i = 0; i < 8; i++) {
- lcd.print(grid[0][i]);
- }
- // Update operator section
- lcd.setCursor(8, 0);
- if (currentMode == MODE_OPERATOR) {
- lcd.print("<");
- } else {
- lcd.print(" ");
- }
- lcd.print(operators[currentOperator]);
- if (currentOperator >= 0 && currentOperator <= 2) {
- lcd.print(" ");
- } else {
- lcd.write(byte(2));
- };
- if (currentOperator == 0 || currentOperator == 3) {
- lcd.print("^");
- } else if (currentOperator == 1 || currentOperator == 4) {
- lcd.write(byte(0));
- } else if (currentOperator == 2 || currentOperator == 5) {
- lcd.write(byte(1));
- };
- if (currentMode == MODE_OPERATOR) {
- lcd.print(">");
- }
- // Update second row - bits
- lcd.setCursor(0, 1);
- for (int i = 0; i < 8; i++) {
- lcd.print(grid[1][i]);
- }
- // Show operation button
- lcd.setCursor(8, 1);
- lcd.print(" OPR8 ");
- }
- else {
- // Result mode - show only result and buttons
- lcd.setCursor(0, 0);
- lcd.print(" REVERT RESET ");
- // Show result on bottom row
- lcd.setCursor(0, 1);
- for (int i = 0; i < 8; i++) {
- lcd.print(grid[1][i]);
- }
- }
- // Restore cursor position based on mode
- if (currentMode == MODE_GRID) {
- lcd.setCursor(cursorX, cursorY);
- }
- else if (currentMode == MODE_OPERATOR) {
- lcd.setCursor(10, 0);
- resetBlink();
- }
- else if (currentMode == MODE_RESULT) {
- lcd.setCursor(3, 0);
- resetBlink();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment