Advertisement
macca-nz

LED_and_SERVO_arrays

Feb 1st, 2021 (edited)
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Servo and Led Array
  2. //4 buttons 1 input pin
  3.  
  4. #include <Servo.h>
  5. #define gates (sizeof(servoPins)/sizeof(int))
  6. #define ledCount (sizeof(leds)/sizeof(int))
  7. const int servoPins[]{3,5,6};
  8. const int leds[]{13,12,11,10,9};
  9. Servo servos[gates];
  10. #define closed 20
  11. #define opened 150
  12. #define fsGates 0
  13. #define cncGates 1
  14. #define msGates 2
  15. #define tsGates 3
  16. int gateState = fsGates;
  17. int servoVal[][gates]{
  18.   {closed, closed, closed},
  19.   {opened, closed, closed},
  20.   {closed, opened, closed},
  21.   {closed, closed, opened},
  22.  };
  23.  
  24. #define LEDSoff 0
  25. #define fsLEDS 1
  26. #define cncLEDS 2
  27. #define msLEDS 3
  28. #define tsLEDS 4
  29. int ledValState = LEDSoff;
  30. int ledVals[][ledCount]{
  31.   {LOW,LOW,LOW,LOW,LOW},
  32.   {HIGH,LOW,LOW,LOW,HIGH},
  33.   {LOW,HIGH,LOW,LOW,HIGH},
  34.   {HIGH,LOW,HIGH,LOW,HIGH},
  35.   {LOW,LOW,LOW,HIGH,HIGH},
  36. };
  37.  
  38. boolean print = false;
  39. int val;
  40. int systemState = LOW;
  41. unsigned long ct = 0;
  42. unsigned long period = 500;
  43.  
  44. void setup()
  45. {
  46. Serial.begin(9600);
  47.   pinMode(A0, INPUT);
  48.   for(int i=0; i<ledCount; i++){
  49.   for(int s=0; s<gates; s++){
  50.   pinMode(leds[i], OUTPUT);
  51.   digitalWrite(leds[i], ledValState);
  52.   servos[s].attach(servoPins[s]);
  53.   servos[s].write(servoVal[fsGates][s]);
  54.   }
  55.  }
  56. }
  57.  
  58. void loop(){
  59.  
  60.   //Creates Button Values
  61.   val = analogRead(A0);
  62.   val = map(val, 0, 1024, 0, 1000);
  63.  
  64.   //Create buttonState
  65.   if(val > 350){
  66.     delay(50);
  67.     if(systemState == HIGH){
  68.       systemState = LOW;
  69.       gateState = fsGates;
  70.       ledValState = LEDSoff;
  71.     }else{
  72.     print = true;
  73.     systemState = HIGH;
  74.     gateState = fsGates;
  75.     ledValState = LEDSoff;
  76.     }
  77.   }else if(val > 200){
  78.     delay(50);
  79.     if(systemState == HIGH){
  80.       systemState = LOW;
  81.       gateState = fsGates;
  82.       ledValState = LEDSoff;
  83.     }else{
  84.     print = true;
  85.     systemState = HIGH;
  86.     gateState = cncGates;
  87.     ledValState = cncLEDS;
  88.     }    
  89.   }else if(val > 140){
  90.     delay(50);
  91.     if(systemState == HIGH){
  92.       systemState = LOW;
  93.       gateState = fsGates;
  94.       ledValState = LEDSoff;
  95.     }else{
  96.       print = true;
  97.       systemState = HIGH;
  98.       gateState = msGates;
  99.       ledValState = msLEDS;
  100.     }  
  101.   }else if(val > 50){
  102.     delay(50);
  103.     if(systemState == HIGH){
  104.       systemState = LOW;
  105.       gateState = fsGates;
  106.       ledValState = LEDSoff;
  107.     }else{
  108.     print = true;
  109.     systemState = HIGH;
  110.     gateState = tsGates;
  111.     ledValState = tsLEDS;
  112.     }
  113.   }
  114.   for(int i=0; i<ledCount; i++){
  115.   for(int s=0; s<gates; s++){
  116.     servos[s].write(servoVal[gateState][s]);
  117.     digitalWrite(leds[i], ledVals[ledValState][i]);
  118.     }
  119.   }  
  120.   Print();
  121.  
  122.  }
  123.  
  124. void Print(){
  125.   if(print == true){
  126.     Serial.print("Button Val:  ");
  127.     Serial.println(val);
  128.     print = false;
  129.   }
  130.  
  131. }
  132.  
  133.  
Advertisement
RAW Paste Data Copied
Advertisement