macca-nz

update(6)

Feb 3rd, 2021 (edited)
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. #define servoCount (sizeof(servosPins)/sizeof(int))
  3. #define ledCount (sizeof(leds)/sizeof(int))
  4.  
  5. const int leds[]{13,12,11,8,7};
  6. const int servosPins[]{9,6,5,3};
  7. Servo servos[servoCount];
  8.  
  9. #define LEDS0 0
  10. #define LEDS1 1
  11. #define LEDS2 2
  12. #define LEDS3 3
  13. #define LEDS4 4
  14. #define SL1 5
  15. #define SL2 6
  16. #define SL3 7
  17. #define SL4 8
  18. int ledState = LEDS0,
  19.     ledStatePrev = ledState;
  20. int SLstate = LEDS0;
  21. int ledVals[][ledCount]{
  22.   {LOW,LOW,LOW,LOW,LOW},
  23.   {HIGH,LOW,LOW,LOW,HIGH},    
  24.   {LOW,HIGH,LOW,LOW,HIGH},
  25.   {LOW,LOW,HIGH,LOW,HIGH},
  26.   {LOW,LOW,LOW,HIGH,HIGH},
  27.   {HIGH,LOW,LOW,LOW,LOW},    
  28.   {LOW,HIGH,LOW,LOW,LOW},
  29.   {LOW,LOW,HIGH,LOW,LOW},
  30.   {LOW,LOW,LOW,HIGH,LOW},
  31. }; /****************  ^^^
  32.             I'm assuming this is the POWER LED?
  33.         To be clear: Do you want this to blink?
  34. */
  35.  
  36. #define GATES_CLOSED 0
  37. #define GATE1 1
  38. #define GATE2 2
  39. #define GATE3 3
  40. #define GATE4 4
  41.  
  42. int gateState = GATES_CLOSED;
  43. int servoVals[][servoCount]{
  44.     {20,20,20,20},
  45.     {160,20,20,20},
  46.     {20,160,20,20},
  47.     {20,20,160,20},
  48.     {20,20,20,160},
  49. };
  50.  
  51. bool print = false, // bool is C standard
  52.      blink = false;
  53. int flash, val;
  54. unsigned long ctOffDelay = 0, ctBlink = 0; // consolidation for readability
  55.  
  56. void setup(){
  57.     Serial.begin(9600);
  58.     pinMode(A0, INPUT);
  59.     for(int i=0; i<ledCount; i++){
  60.         pinMode(leds[i], OUTPUT);
  61.         digitalWrite(leds[i], ledVals[ledState][i]);
  62.     }
  63.     for(int s=0; s<servoCount; s++){
  64.         servos[s].attach(servosPins[s]);
  65.         servos[s].write(servoVals[gateState][s]);
  66.     }
  67. }
  68.  
  69. /// - Consolidate - See my comment about redundancy below -
  70. void updateGate(int newGateState, int newLedState){
  71.  
  72.     delay(50); /// I still highly suggest replacing this with a millis() check instead
  73.  
  74.     if(gateState == newGateState){
  75.         //gateState = GATES_CLOSED;
  76.         flash = ledState;
  77.         blink = true;
  78.         ctOffDelay = millis();
  79.     } else {
  80.         gateState = newGateState;
  81.         ledState = newLedState;
  82.     }
  83.     // This is the only way I could make the LED work correctly
  84.     if (flash == LEDS1){
  85.         SLstate = SL1;
  86.     } else if (flash == LEDS2){
  87.         SLstate = SL2;
  88.     } else if (flash == LEDS3){
  89.         SLstate = SL3;
  90.     } else if (flash == LEDS4){
  91.         SLstate = SL4;
  92.     }
  93.     print = true;
  94. }
  95.  
  96. void loop(){
  97.  
  98.     //Creates Button Values
  99.     val = analogRead(A0);
  100.     val = map(val, 0, 1024, 0, 1000);
  101.  
  102.     if(val > 500){
  103.         updateGate(GATE1, LEDS1);
  104.     } else if (val > 300){
  105.         updateGate(GATE2, LEDS2);
  106.     } else if (val > 200){
  107.         updateGate(GATE3, LEDS3);
  108.     } else if(val > 10){
  109.         updateGate(GATE4, LEDS4);
  110.     }
  111.     /* ^^^^^^ Please don't be redundant ^^^^^^^
  112.         If a piece of code seems to show up a lot, there's a chance you can shorten your code's redundancy.
  113.         Do this to make it easier to read
  114.         I've put the all the gate changing code into a function. This greatly consolidates your code.
  115.     */
  116.  
  117.     // This blink operation here works - but I know a few ways to improve it so you can have multiple LEDS blink independently
  118.     if(blink == true){
  119.         gateState = GATES_CLOSED;
  120.             if((millis() - ctOffDelay) > 1200){
  121.                 blink = false;
  122.                 ledState = LEDS0;
  123.                 SLstate = LEDS0;
  124.                 //ctOffDelay = millis(); // not necessary because it must be reset somewhere else
  125.             }
  126.  
  127.                     if((millis() - ctBlink) > 200){
  128.                         ledState = flash;
  129.                         ctBlink = millis();
  130.                             } else if((millis() - ctBlink) > 100){
  131.                                 ledState = SLstate;
  132.                     }
  133.     }
  134.  
  135.     if(print == true){
  136.         Serial.print("Button Val:  ");
  137.         Serial.print(val);
  138.         Serial.print("    Current State:  ");
  139.         Serial.println(gateState);
  140.         print = false;
  141.     }
  142.  
  143.     /* You should only need to run this code when the ledState changes.
  144.         Running this code in the loop() call is costing unnecessary performance
  145.     */
  146.     if(ledState != ledStatePrev){
  147.         for(int i=0; i<ledCount; i++){
  148.             digitalWrite(leds[i], ledVals[ledState][i]);
  149.         }
  150.         ledStatePrev = ledState; // ensures that this does not waste performance
  151.     }
  152.  
  153.     for(int s=0; s<servoCount; s++){
  154.         servos[s].write(servoVals[gateState][s]);
  155.     }
  156. }
  157.  
RAW Paste Data Copied