/* * Fab academy 2020 * Author: Anssi Heikkinen * This is a program for ButtonBox which is documented on the http://fabacademy.org/2020/labs/oulu/students/anssi-heikkinen/finalproject.html * */ // adding libraries #include #include // activating joystik library Joystick_ Joystick; // setting up lcd + a button for controlling it LiquidCrystal lcd(10,6,9,12,8,4); const int lcdButton = 11; int lcdButtonState = 0; /* not currently using this const int resetButton; */ // setting up pins for buttons and potentiometers const int buttonPin1 = 0; const int buttonPin2 = 1; const int buttonPin3 = 2; const int buttonPin4 = 3; const int buttonPin5 = 13; const int buttonPin6 = 5; // const int potPin1 = A0; const int potPin2 = A1; const int potPin3 = A2; // Setting up values for potentiometers int value1 = 0; int value2 = 0; int value3 = 0; int prevValue1 = 0; int prevValue2 = 0; int prevValue3 = 0; void setup() { // opening serial for testing purposes, joystick for pushbuttons and potentiometers Serial.begin(9600); Joystick.begin(); lcd.begin(16,2); // setting lcdButton as input, pushbuttons don't have to be set up this way because of joystick pinMode(lcdButton, INPUT); } void loop() { // first setting up buttons to read if it's pressed. If pressed it activates the joystick.pressButton and it releases it after it has been pressed. All buttons are on releaseButton-state when they are not pressed. int button1 = digitalRead(buttonPin1); int button2 = digitalRead(buttonPin2); int button3 = digitalRead(buttonPin3); int button4 = digitalRead(buttonPin4); int button5 = digitalRead(buttonPin5); int button6 = digitalRead(buttonPin6); if (button1 == 0){ Joystick.pressButton(buttonPin1); Serial.println("1"); } else { Joystick.releaseButton(buttonPin1); } if (button2 == 0){ Joystick.pressButton(buttonPin2); Serial.println("2"); } else { Joystick.releaseButton(buttonPin2); } if (button3 == 0){ Joystick.pressButton(buttonPin3); Serial.println("3"); } else { Joystick.releaseButton(buttonPin3); } if (button4 == 0){ Joystick.pressButton(buttonPin4); Serial.println("4"); } else { Joystick.releaseButton(buttonPin4); } if (button5 == 0){ Joystick.pressButton(buttonPin5); Serial.println("5"); } else { Joystick.releaseButton(buttonPin5); } if (button6 == 0){ Joystick.pressButton(buttonPin6); Serial.println("6"); } else { Joystick.releaseButton(buttonPin6); } // tested something new with lcd which was in this case scrolling text from right to left. lcdButtonState = digitalRead(lcdButton); if (lcdButtonState == HIGH) { lcd.setCursor(0,0); lcd.print("Fab academy 2020"); } else { lcd.setCursor(0,0); lcd.println("Final project - Buttonbox"); for (int positionCounter = 0; positionCounter < 9; positionCounter++){ lcd.scrollDisplayLeft(); delay(500); if(positionCounter == 8){ lcd.clear(); } else{ } } } // lastly potentiometers. These will work as buttons, but will trigger when rotating the potentiometer and the value is 50 lower or higher than the previous value which activated a press value1 = analogRead(potPin1); if ((value1 <= prevValue1 - 50) || (value1 >= prevValue1 + 50)){ prevValue1 = value1; Joystick.pressButton(potPin1); } else { Joystick.releaseButton(potPin1); } value2 = analogRead(potPin2); if ((value2 <= prevValue2 - 50) || (value2 >= prevValue2 + 50)){ prevValue2 = value2; Joystick.pressButton(potPin2); } else { Joystick.releaseButton(potPin2); } value3 = analogRead(potPin3); if ((value3 <= prevValue3 - 50) || (value3 >= prevValue3 + 50)){ prevValue3 = value3; Joystick.pressButton(potPin3); } else { Joystick.releaseButton(potPin3); } }