Guest User

Untitled

a guest
Jun 23rd, 2021
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. int button = 9;
  3. long debounce = 500; // the debounce time
  4. long time = 0;
  5. int buttonCount = 0;
  6. int DPlus1 = 5;
  7. int DPlus2 = 4;
  8. int DMinus1 = 3;
  9. int DMinus2 = 2;
  10. int RLED = 6;
  11. int GLED = 7;
  12. int BLED = 8;
  13. int vRead;
  14. unsigned long previousMillis = 0;
  15.  
  16. void setup() {
  17. pinMode(button, INPUT_PULLUP);
  18. pinMode(RLED, OUTPUT);
  19. pinMode(GLED, OUTPUT);
  20. pinMode(BLED, OUTPUT);
  21. // Serial.begin(9600);
  22.  
  23. // Serial.println("5v"); //button isn't trigger when powered on, so set for 5v in setup
  24. pinMode(DPlus1, INPUT);
  25. pinMode(DPlus2, OUTPUT);
  26. pinMode(DMinus1, INPUT);
  27. pinMode(DMinus2, INPUT);
  28. digitalWrite(DPlus2, HIGH);
  29.  
  30. digitalWrite(RLED, HIGH); //Write these high to avoid them turning on before vRead can assess
  31. digitalWrite(GLED, HIGH);
  32. digitalWrite(BLED, HIGH);
  33. }
  34.  
  35. void loop() {
  36.  
  37. if (millis() - previousMillis >= 500) { //check vcc through divider; set LED accordingly
  38. previousMillis = millis();
  39.  
  40. int vRead = analogRead(A0);
  41.  
  42. if ((vRead >= 350) && (vRead <= 450)) {
  43. analogWrite(GLED, 200);
  44. digitalWrite(BLED, HIGH);
  45. digitalWrite(RLED, HIGH);
  46. }
  47. if ((vRead >= 451) && (vRead <= 750)) {
  48. digitalWrite(GLED, HIGH);
  49. digitalWrite(BLED, LOW);
  50. digitalWrite(RLED, HIGH);
  51. }
  52. if ((vRead >= 890) && (vRead <= 970)) {
  53. digitalWrite(GLED, HIGH);
  54. digitalWrite(BLED, HIGH);
  55. digitalWrite(RLED, LOW);
  56. }
  57.  
  58. if (((vRead >= 451) && (vRead <= 750)) && (buttonCount == 2)){ //case where 12v is not supported, but 5v and 9v are
  59. buttonCount ++;
  60. }
  61. }
  62. if (digitalRead(button) == LOW && millis() - time > debounce) { //Pushbutton for voltage switching
  63. buttonCount++;
  64. if (buttonCount >= 4) { //Change based on supply modes available
  65. buttonCount = 1;
  66. }
  67. time = millis();
  68. }
  69. if (buttonCount == 1) { //since we set the 5v in setup()
  70. DPlus_three_v_three(); //9v on first press
  71. DMinus_sixhundred_mA();
  72. }
  73.  
  74. else if (buttonCount == 2) { //12v
  75. DPlus_sixhundred_mA();
  76. DMinus_sixhundred_mA();
  77. }
  78.  
  79. else if (buttonCount == 3) { //5v
  80. DPlus_sixhundred_mA();
  81. DMinus_GND();
  82. }
  83. }
  84.  
  85. //Collection of pin states required for voltage dividers
  86. void DPlus_sixhundred_mA() {
  87. pinMode(DPlus1, INPUT);
  88. pinMode(DPlus2, OUTPUT);
  89. digitalWrite(DPlus2, HIGH);
  90. }
  91. void DPlus_three_v_three() {
  92. pinMode(DPlus1, OUTPUT);
  93. pinMode(DPlus2, OUTPUT);
  94. digitalWrite(DPlus1, HIGH);
  95. digitalWrite(DPlus2, HIGH);
  96. }
  97. void DMinus_GND() {
  98. pinMode(DMinus1, INPUT);
  99. pinMode(DMinus2, INPUT);
  100. }
  101. void DMinus_sixhundred_mA() {
  102. pinMode(DMinus1, INPUT);
  103. pinMode(DMinus2, OUTPUT);
  104. digitalWrite(DMinus2, HIGH);
  105. }
  106. void DMinus_three_v_three() {
  107. pinMode(DMinus1, OUTPUT);
  108. pinMode(DMinus2, OUTPUT);
  109. digitalWrite(DMinus1, HIGH);
  110. digitalWrite(DMinus2, HIGH);
  111. }
RAW Paste Data