Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit serial stoplight sketch - 25 October 2019
- This sketch is supposed to change three single-color LEDs (red-green-blue) like a stoplight.
- Board - not specified. I test-ran it on a Nano.
- Also there is some serial messages added to verify the whole thing works in the serial monitor, since I didn't hook up the three LEDs.
- */
- int led1 = 13;
- int led2 = 12;
- int led3 = 11;
- char temp_character = 'x'; // This is for input testing. The input character first gets stored in this, and then if it is a valid character, it gets moved to input_character below.
- char input_character = 'x'; // I declared a global with a better name here....
- void setup() {
- Serial.begin(9600);
- pinMode(led1, OUTPUT);
- pinMode(led2, OUTPUT);
- pinMode(led3, OUTPUT);
- Serial.println("Exiting setup()"); // If you use serial messages anyway, it is good to have a message at the end of setup()
- // If there is an error in setup(), the processor may just skip the rest of setup() and continue on to the main loop, and experience errors there.
- // So if you don't see this message, it tells you that something odd happened during the setup() function.
- }
- void loop() {
- if (Serial.available() > 0) {
- // The code to get the input character has been moved into a function, because you will need to call it from two different places, but it is the same code.
- // And that's what functions are for.
- get_new_input_character(); // This function is at the end of the sketch.
- if (input_character == 'A') {
- set_led_values(1, 1, 1); // I made a function to set all these values, since all three are always set at the same time anyway.
- Serial.println("Option 'A' has run");
- // To make sure this only runs once, after it has run you can change the value of input_character to something that will not allow any of the A, B or C states.
- input_character = 'x';
- // (this section runs twice if you comment out the above line, since there is TWO characters sent {the letter and a null character)
- // and the null character does not re-write the value of input_character.
- }
- while (input_character == 'B') {
- // This section will continue looping after it is triggered, until another valid character is entered.
- Serial.println("Option 'B' is started");
- set_led_values(1, 0, 0);
- delay(1000);
- Serial.println("Option 'B' section #1");
- set_led_values(0, 1, 0);
- delay(1000);
- Serial.println("Option 'B' section #2");
- set_led_values(0, 0, 1);
- delay(1000);
- Serial.println("Option 'B' section #3");
- Serial.println("Option 'B' is ended");
- // You don't need the lines below, because the only way that the processor could get into this part of the code is if input_character was equal to 'B'
- // And the sketch isn't going to keep re-checking the serial messages, since that call is not inside this section of code.
- //if (input_character != 'B')
- // break;
- // What you need is below: after this section has run one time, you need to check again for any serial message.
- if (Serial.available() > 0) {
- get_new_input_character(); // If there is any serial message, then you get the new input character.
- }
- }
- if (input_character == 'C') {
- set_led_values(0, 0, 0);
- Serial.println("Option 'C' has run");
- // To make sure this only runs once, after it has run you can change the value of input_character to something that will not allow any of the A, B or C states.
- input_character = 'x';
- // (this section runs twice if you comment out the above line, since there is TWO characters sent {the letter and a null character)
- // and the null character does not re-write the value of input_character.
- }
- }
- }
- void set_led_values(int ledA, int ledB, int ledC) {
- // This is just a function to write all three LED values easier, since those lines are repeated several times.
- digitalWrite(led1, ledA);
- digitalWrite(led2, ledB);
- digitalWrite(led3, ledC);
- }
- void get_new_input_character() {
- // When you use serial messages, it is a good idea to perform input filtering to make sure that your program ONLY catches the characters that you wanted it to respond to.
- // Some serial programs may or may not send a string-ending character or a line-ending character, and you don't want your program to respond to either of those.
- // So you need code to make sure that the program only catches the important characters, and rejects any other ones.
- while (Serial.available() > 0) {
- temp_character = Serial.read();
- if (temp_character == 'A' || temp_character == 'B' || temp_character == 'C') {
- input_character = temp_character; // If the entered character is A, B, or C, then it gets saved.
- }
- }
- }
- // ~~~~~~~~ the end ~~~~~~~~
Add Comment
Please, Sign In to add comment