skizziks_53

Reddit 3-led serial controller v 1.0

Oct 25th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. /*
  2.   Reddit serial stoplight sketch - 25 October 2019
  3.  
  4.   This sketch is supposed to change three single-color LEDs (red-green-blue) like a stoplight.
  5.   Board - not specified. I test-ran it on a Nano.
  6.   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.
  7. */
  8.  
  9.  
  10. int led1 = 13;
  11. int led2 = 12;
  12. int led3 = 11;
  13.  
  14. 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.
  15. char input_character = 'x'; // I declared a global with a better name here....
  16.  
  17. void setup() {
  18.   Serial.begin(9600);
  19.   pinMode(led1, OUTPUT);
  20.   pinMode(led2, OUTPUT);
  21.   pinMode(led3, OUTPUT);
  22.   Serial.println("Exiting setup()"); // If you use serial messages anyway, it is good to have a message at the end of setup()
  23.   // 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.
  24.   // So if you don't see this message, it tells you that something odd happened during the setup() function.
  25. }
  26.  
  27.  
  28.  
  29. void loop() {
  30.  
  31.   if (Serial.available() > 0) {
  32.  
  33.     // 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.
  34.     // And that's what functions are for.
  35.     get_new_input_character(); // This function is at the end of the sketch.
  36.  
  37.     if (input_character == 'A') {
  38.       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.
  39.       Serial.println("Option 'A' has run");
  40.       // 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.
  41.       input_character = 'x';
  42.       // (this section runs twice if you comment out the above line, since there is TWO characters sent {the letter and a null character)
  43.       //      and the null character does not re-write the value of input_character.
  44.     }
  45.  
  46.     while (input_character == 'B') {
  47.       // This section will continue looping after it is triggered, until another valid character is entered.
  48.       Serial.println("Option 'B' is started");
  49.       set_led_values(1, 0, 0);
  50.       delay(1000);
  51.       Serial.println("Option 'B' section #1");
  52.       set_led_values(0, 1, 0);
  53.       delay(1000);
  54.       Serial.println("Option 'B' section #2");
  55.       set_led_values(0, 0, 1);
  56.       delay(1000);
  57.       Serial.println("Option 'B' section #3");
  58.       Serial.println("Option 'B' is ended");
  59.  
  60.       // 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'
  61.       // And the sketch isn't going to keep re-checking the serial messages, since that call is not inside this section of code.
  62.       //if (input_character != 'B')
  63.       //  break;
  64.  
  65.       // What you need is below: after this section has run one time, you need to check again for any serial message.
  66.       if (Serial.available() > 0) {
  67.         get_new_input_character(); // If there is any serial message, then you get the new input character.
  68.       }
  69.     }
  70.  
  71.     if (input_character == 'C') {
  72.       set_led_values(0, 0, 0);
  73.       Serial.println("Option 'C' has run");
  74.       // 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.
  75.       input_character = 'x';
  76.       // (this section runs twice if you comment out the above line, since there is TWO characters sent {the letter and a null character)
  77.       //      and the null character does not re-write the value of input_character.
  78.     }
  79.  
  80.   }
  81. }
  82.  
  83.  
  84. void set_led_values(int ledA, int ledB, int ledC) {
  85.   // This is just a function to write all three LED values easier, since those lines are repeated several times.
  86.   digitalWrite(led1, ledA);
  87.   digitalWrite(led2, ledB);
  88.   digitalWrite(led3, ledC);
  89. }
  90.  
  91.  
  92. void get_new_input_character() {
  93.   // 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.
  94.   // 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.
  95.   // So you need code to make sure that the program only catches the important characters, and rejects any other ones.
  96.   while (Serial.available() > 0) {
  97.     temp_character = Serial.read();
  98.     if (temp_character == 'A' || temp_character == 'B' || temp_character == 'C') {
  99.       input_character = temp_character; // If the entered character is A, B, or C, then it gets saved.
  100.     }
  101.   }
  102. }
  103.  
  104.  
  105.  
  106. // ~~~~~~~~ the end ~~~~~~~~
Add Comment
Please, Sign In to add comment