// EPOCH PROJECT#5 "INPUTS AND OUTPUTS 5 WITH FUNCTIONS" // THIS PROJECT BUILDS OFF OF PROJECT#4. WE'RE GOING TO USE THE SAME CODE USED IN PROJECT#4, BUT WE'RE GOING TO DO THINGS DIFFERENTLY THIS TIME AROUND. WE'RE GOING TO USE FUNCTIONS!!! // CONNECT THE RLED (RED LED) PIN TO GPIO PIN#2 ON CHIP(A) // CONNECT THE YLED (YELLOW LED) PIN TO GPIO PIN#4 ON CHIP(A) // CONNECT THE GLED(GREEN LED) PIN TO GPIO PIN#5 ON CHIP(A) // CONNECT THE NL "NORMALLY LOW" BUTTON OUTPUT PIN TO GPIO PIN 3 ON CHIP(A). THE VOLTAGE ON THIS PIN IS 0V (LOW) WHEN NOT PRESSED. WHEN THE BUTTON IS PRESSED AND HELD DOWN, THE VOLTAGE ON THIS PIN WILL BE 5V(HIGH) #define REDLED 2 // LET'S CALL GPIO#2 BY THE NAME "REDLED". #define YELLOWLED 4 // LET'S CALL GPIO#4 BY THE NAME "YELLOWLED". #define GREENLED 5 // LET'S CALL GPIO#5 BY THE NAME "GREENLED". #define NL 3 // LET'S CALL GPIO#3 BY THE NAME "NL" FOR "NORMALLY LOW BUTTON" void setup() { pinMode(REDLED,OUTPUT); // SET "REDLED" AS AN OUTPUT pinMode(YELLOWLED,OUTPUT); // SET "YELLOWLED" AS AN OUTPUT pinMode(GREENLED,OUTPUT); // SET "GREENLED" AS AN OUTPUT pinMode(NL,INPUT); // SET "NL" AS AN INPUT. WE CAN NOW INSTRUCT THE CODE TO LOOK AT THE HIGH (5 VOLT) OR LOW (0 VOLT) LOGIC ON THIS INPUT. } void loop() // WHEN THE NL BUTTON IS PRESSED, A "FOR LOOP" WILL BE EXECUTED. WE'LL TALK ABOUT THAT MORE DOWN BELOW. { buttonpress(); // This is a function that we're going to call. We're going to JUMP to this function seen below (void buttonpress). When the user presses the button, and lets go, the buttonpress function ends, and then the program jumps back here. forfunction(); // This function is the FOR LOOP that turns the LEDs on and off in sequence. If we want do this more than once in a piece of code, we can just call this function, and we'll jump below to "void forfunction() function" // Once the forfunction function is done, the code will jump back here, becase it is next in the sequence. This happens to be the end of the void loop, so we go back to the top to call "buttonpress(); again. } void buttonpress(){ // THIS CODE WAS USED IN PROJECT#4. WE'VE TURNED IT INTO A REUSABLE FUNCTION THAT CAN BE CALLED ANYTIME. if(digitalRead(NL) == HIGH) // THIS IS AN "if" STATEMENT. IF THE CONDITIONS IN THIS STATEMENT ARE MET, THEN THE FOLLOWING CODE WILL BE EXECUTED. THE DIGITALREAD COMMAND READS THE STATE OF THE NL PIN. IF PRESSED (HIGH), FLASH THE LED FOR ONE SECOND. { delay(50); // 50ms DELAY to compensate for button debounce. while(digitalRead(NL) == HIGH){ // LET'S SAY THAT WE WANT TO MAKE IT SO THAT YOU NEED TO PRESS THE BUTTON, AND THEN LET GO IN ORDER FOR THE CODE TO PROGRESS. THIS WHILE LOOP MAKES IT SO THAT THE CODE CANNOT PROGRESS UNTIL YOU LET GO OF THE BUTTON. SEE NOTES. {} } } } void forfunction(){ for (int i = 0; i < 5; i++) // THIS FOR LOOP WAS USED IN PROJECTS 3 AND 4. WE'VE NOW TURNED IT INTO IT'S OWN REUSABLE FUNCTION OR "SUB-ROUTINE" { digitalWrite(REDLED,HIGH); // THOS FOLLOWING SECTION WILL EXEUTE 5X TIMES BASED ON THE SETTINGS IN THE ABOVE FOR LOOP. TURN THE RED LED ON FOR 200ms. delay(200); digitalWrite(YELLOWLED,HIGH); // AFTER 200ms, TURN ON THE YELLOW LED delay(200); digitalWrite(GREENLED,HIGH); // AFTER ANOTHER 200ms, TURN ON THE GREEN LED delay(200); digitalWrite(GREENLED,LOW); // THEN TURN OFF ALL OF THE LEDS WITH 200ms DELAYS IN BEWEEN EACH STEP delay(200); digitalWrite(YELLOWLED,LOW); delay(200); digitalWrite(REDLED,LOW); delay(200); } } // NOTES: // Functions make things easier for many reasons. When you press the compile/verify button, the Arduino checks syntax and format to ensure that everyting is hunky dory. If you're missing a ";" or " { } " anywhere, the compiler might not necessarily // aim you in the right direction. Breaking the code down into functions makes this easier to troubleshoot. It also saves lots of code space, as once a function is created, it can be called on over and over and over, which makes your // void loop easier to troubleshoot when compiler problems are found.