/* turn2sec - Paul Meyer Reimer * A simple menu. On the LCD shield, select "3 Move motor" which calls moveMoter( ). * This should make your stepper motor move for 2 seconds and then stop. * (The menu won't respond to any button presses. It just returns after the motion is done.) */ /* 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 #include PhysicsMenu m = PhysicsMenu(); // This line creates as instance "m" of the Menu object type PhysicsMenu long velocity=0; // speed of motor // initialize a tic object TicI2C tic; // ---------------------------------------------------------------------------------- void setup() { // Here is where you put lines that execute just once. Like setting input and output. Wire.begin(); delay(20); // Define current motor position to be zero. tic.haltAndSetPosition(0); Serial.begin(9600); m.begin(); // this starts up the menu m.addItem("2 Print Stuff ", &printStuff); // For each menu item, you give the text of the menu label m.addItem("3 Move motor ", &moveMotor); // and then the name of the function to be called when you select it 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("Velocity = "); m.lcd.print(velocity); delay(2000); } // Adjust a number using the up and down buttons --------------------------- void moveMotor() { m.lcd.clear(); while(1) { m.lcd.home(); m.lcd.print("Velocity = "); m.lcd.print(velocity); m.lcd.print(" "); tic.setTargetVelocity(2000000); // start turning at this velocity delay(2000); tic.setTargetVelocity(0); // Stops the motion return; //LEAVES the loop here (the code below never executes) 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) {velocity++;} // Up button increments menu option if (buttons == BUTTON_DOWN) {velocity--;} // Down button decrements menu option if (buttons == BUTTON_LEFT) {return;} } }