Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #define servoCount (sizeof(servosPins)/sizeof(int))
- #define ledCount (sizeof(leds)/sizeof(int))
- const int leds[]{13,12,11,8,7};
- const int servosPins[]{9,6,5,3};
- Servo servos[servoCount];
- #define LEDS0 0
- #define LEDS1 1
- #define LEDS2 2
- #define LEDS3 3
- #define LEDS4 4
- #define SL1 5
- #define SL2 6
- #define SL3 7
- #define SL4 8
- int ledState = LEDS0,
- ledStatePrev = ledState;
- int SLstate = LEDS0;
- int ledVals[][ledCount]{
- {LOW,LOW,LOW,LOW,LOW},
- {HIGH,LOW,LOW,LOW,HIGH},
- {LOW,HIGH,LOW,LOW,HIGH},
- {LOW,LOW,HIGH,LOW,HIGH},
- {LOW,LOW,LOW,HIGH,HIGH},
- {HIGH,LOW,LOW,LOW,LOW},
- {LOW,HIGH,LOW,LOW,LOW},
- {LOW,LOW,HIGH,LOW,LOW},
- {LOW,LOW,LOW,HIGH,LOW},
- }; /**************** ^^^
- I'm assuming this is the POWER LED?
- To be clear: Do you want this to blink?
- */
- #define GATES_CLOSED 0
- #define GATE1 1
- #define GATE2 2
- #define GATE3 3
- #define GATE4 4
- int gateState = GATES_CLOSED;
- int servoVals[][servoCount]{
- {20,20,20,20},
- {160,20,20,20},
- {20,160,20,20},
- {20,20,160,20},
- {20,20,20,160},
- };
- bool print = false, // bool is C standard
- blink = false;
- int flash, val;
- unsigned long ctOffDelay = 0, ctBlink = 0; // consolidation for readability
- void setup(){
- Serial.begin(9600);
- pinMode(A0, INPUT);
- for(int i=0; i<ledCount; i++){
- pinMode(leds[i], OUTPUT);
- digitalWrite(leds[i], ledVals[ledState][i]);
- }
- for(int s=0; s<servoCount; s++){
- servos[s].attach(servosPins[s]);
- servos[s].write(servoVals[gateState][s]);
- }
- }
- /// - Consolidate - See my comment about redundancy below -
- void updateGate(int newGateState, int newLedState){
- delay(50); /// I still highly suggest replacing this with a millis() check instead
- if(gateState == newGateState){
- //gateState = GATES_CLOSED;
- flash = ledState;
- blink = true;
- ctOffDelay = millis();
- } else {
- gateState = newGateState;
- ledState = newLedState;
- }
- // This is the only way I could make the LED work correctly
- if (flash == LEDS1){
- SLstate = SL1;
- } else if (flash == LEDS2){
- SLstate = SL2;
- } else if (flash == LEDS3){
- SLstate = SL3;
- } else if (flash == LEDS4){
- SLstate = SL4;
- }
- print = true;
- }
- void loop(){
- //Creates Button Values
- val = analogRead(A0);
- val = map(val, 0, 1024, 0, 1000);
- if(val > 500){
- updateGate(GATE1, LEDS1);
- } else if (val > 300){
- updateGate(GATE2, LEDS2);
- } else if (val > 200){
- updateGate(GATE3, LEDS3);
- } else if(val > 10){
- updateGate(GATE4, LEDS4);
- }
- /* ^^^^^^ Please don't be redundant ^^^^^^^
- If a piece of code seems to show up a lot, there's a chance you can shorten your code's redundancy.
- Do this to make it easier to read
- I've put the all the gate changing code into a function. This greatly consolidates your code.
- */
- // This blink operation here works - but I know a few ways to improve it so you can have multiple LEDS blink independently
- if(blink == true){
- gateState = GATES_CLOSED;
- if((millis() - ctOffDelay) > 1200){
- blink = false;
- ledState = LEDS0;
- SLstate = LEDS0;
- //ctOffDelay = millis(); // not necessary because it must be reset somewhere else
- }
- if((millis() - ctBlink) > 200){
- ledState = flash;
- ctBlink = millis();
- } else if((millis() - ctBlink) > 100){
- ledState = SLstate;
- }
- }
- if(print == true){
- Serial.print("Button Val: ");
- Serial.print(val);
- Serial.print(" Current State: ");
- Serial.println(gateState);
- print = false;
- }
- /* You should only need to run this code when the ledState changes.
- Running this code in the loop() call is costing unnecessary performance
- */
- if(ledState != ledStatePrev){
- for(int i=0; i<ledCount; i++){
- digitalWrite(leds[i], ledVals[ledState][i]);
- }
- ledStatePrev = ledState; // ensures that this does not waste performance
- }
- for(int s=0; s<servoCount; s++){
- servos[s].write(servoVals[gateState][s]);
- }
- }
Add Comment
Please, Sign In to add comment