/* Arduino Display Menu Template for GC Electronics Class * Written by Peter Miller and John Buschert in 2015 * * Requires an Adafruit LCD Display Shield * */ // --------------------------------------------------------------------------------- #include // The Display Shield needs Wire for its I2C communication. It comes with Arduino #include // Then Adafruit provides this library to talk to the shield. Find this using the Library Manager (or at Adafruit) #include // This is is our library used by this menu template. Find this at GC PhysicsMenu m = PhysicsMenu(); // This line creates as instance "m" of the Menu object type PhysicsMenu int somenumber; // adjusted by setValue float level; // used by readAnalog // ---------------------------------------------------------------------------------- void setup() { // Here is where you put lines that execute just once. Like setting input and output. pinMode(13, OUTPUT); //sets the pin mode of the LED to OUTPUT Serial.begin(9600); m.begin(); // this starts up the menu m.addItem("1 Flash LED ", &flashLED); // Here is where you put the menu items. m.addItem("1.5 Flash 2 x ", &flashTwice); m.addItem("2 Print Stuff ", &printStuff); // For each menu item, you give the text of the menu label m.addItem("3 Set Value ", &setValue); // and then the name of the function to be called when you select it m.addItem("4 Read Voltage", &readVoltage); // Put an & before the function name. m.addItem("5 Beep 1 kHz ", &beeper); m.addItem("6 Do Nothing ", &doNothing); m.lcd.setBacklight(0x1); // Turns on the backlight. Will be done by default in next version of physMenu } // ----------------------------------------------------------------------------------- void loop() { // This is where you usually put your main code but for this menu system it's just one line. m.dispatch(); // Checks the buttons and updates the display or calls a function depanding on what you press } // ============================================== Functions ============================== // Each function should begin by clearing the display, then printing its name, then pausing briefly // Somewhere it must have a way back either automatically after it's done or in a loop which checks for the left arrow key // Flash the LED once, then return ------------------------------ void flashLED() { m.lcd.clear(); m.lcd.print("Flashing"); pinMode(13,OUTPUT); digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a bit digitalWrite(13, LOW); // set the LED off } // Print something on the LCD ------------------------------------------- void printStuff() { m.lcd.clear(); m.lcd.print("Hello World"); m.lcd.setCursor(0,1); //Cursor to row 0 (of 0 to 15) and line 1 (of 0 and 1) m.lcd.print("Number = "); m.lcd.print(somenumber); delay(2000); } // Adjust a number using the up and down buttons --------------------------- void setValue() { m.lcd.clear(); while(1) { m.lcd.home(); m.lcd.print("Number = "); m.lcd.print(somenumber); m.lcd.print(" "); delay(100); // This slows counting when a button is held down byte buttons = m.getButtons(); // 'buttons' value depends on what was presssed if (buttons == BUTTON_UP) {somenumber++;} // Up button increments menu option if (buttons == BUTTON_DOWN) {somenumber--;} // Down button decrements menu option if (buttons == BUTTON_LEFT) {return;} } } // read the analog value of A0 -------------- void readVoltage() { m.lcd.clear(); while(1) { level = analogRead(0); // Read analog value of pin A0 m.lcd.setCursor(0,0); m.lcd.print("value1 = "); m.lcd.print(level); // Print it on LCD display m.lcd.print(" "); byte buttons = m.getButtons(); // 'buttons' value depends on what was presssed if (buttons == BUTTON_LEFT) {break;} } } // Make a constant beep at 1 kHz ---------------- void beeper() { m.lcd.clear(); m.lcd.print("Beeping 1 kHz"); pinMode(2,OUTPUT); while(1) { digitalWrite(2,HIGH); delayMicroseconds(500); digitalWrite(2,LOW); delayMicroseconds(500); byte buttons = m.getButtons(); // 'buttons' value depends on what was presssed if (buttons == BUTTON_LEFT) {break;} } } // Do Nothing Function -------------- void doNothing() { m.lcd.clear(); delay(500); } void flashTwice() { m.lcd.clear(); m.lcd.print("Flashing"); pinMode(13,OUTPUT); digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a bit digitalWrite(13, LOW); // set the LED off delay(2000); digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a bit digitalWrite(13, LOW); // set the LED off }