SHARE
TWEET

Arduino Blinking LEDs on input

a guest Jul 18th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int currentLedId = 0;
  2.  
  3. int targetLedPowerState = 0;
  4. int currentLedPowerState = 0;
  5.  
  6. float timer = 0;
  7.  
  8. void setup() {
  9.   pinMode(7, OUTPUT);
  10.   pinMode(8, OUTPUT);
  11.   pinMode(9, OUTPUT);
  12.  
  13.   Serial.begin(9600);
  14. }
  15.  
  16.  
  17. void loop() {
  18.   if (Serial.available() > 0 ) {
  19.     char incoming = Serial.read();
  20.     Serial.flush();
  21.     Serial.println(incoming);
  22.  
  23.     processIncomingMessage(incoming);
  24.   }
  25.  
  26.   blink();
  27. }
  28.  
  29. void blink() {
  30.   timer += 0.01;
  31.  
  32.   if (timer > 1000) {
  33.     Serial.println("1000/0.01 ticks is out");
  34.     Serial.println("targetLedPowerState: " + String(targetLedPowerState));
  35.     Serial.println("currentLedPowerState: " + String(currentLedPowerState));
  36.     Serial.println("currentLedId: " + String(currentLedId));
  37.    
  38.     if (targetLedPowerState == 0) {
  39.       turnLedsOff();
  40.     } else if (targetLedPowerState == 1) {
  41.       if (currentLedPowerState == 0) {
  42.         turnLedOn(currentLedId);
  43.         currentLedPowerState = 1;
  44.       } else if (currentLedPowerState == 1) {
  45.         turnLedsOff();
  46.         currentLedPowerState = 0;
  47.       }
  48.     }
  49.  
  50.     timer = 0;
  51.   }
  52. }
  53.  
  54. void processIncomingMessage(char message) {
  55.   switch (message) {
  56.     case '0':
  57.       currentLedId = 7;
  58.       targetLedPowerState = 1;
  59.       break;
  60.     case '1':
  61.       currentLedId = 8;
  62.       targetLedPowerState = 1;
  63.       break;
  64.     case '2':
  65.       currentLedId = 9;
  66.       targetLedPowerState = 1;
  67.       break;
  68.     case '3':
  69.       break;
  70.     case 'f':
  71.       currentLedId = 0;
  72.       targetLedPowerState = 0;
  73.       break;
  74.   }
  75. }
  76.  
  77. void turnLedOn (int ledPin) {
  78.   turnLedsOff();
  79.   digitalWrite(ledPin, 1);
  80. }
  81.  
  82. void turnLedsOff () {
  83.   digitalWrite(7, 0);
  84.   digitalWrite(8, 0);
  85.   digitalWrite(9, 0);
  86. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top