Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int button = 9;
- long debounce = 500; // the debounce time
- long time = 0;
- int buttonCount = 0;
- int DPlus1 = 5;
- int DPlus2 = 4;
- int DMinus1 = 3;
- int DMinus2 = 2;
- int RLED = 6;
- int GLED = 7;
- int BLED = 8;
- int vRead;
- unsigned long previousMillis = 0;
- void setup() {
- pinMode(button, INPUT_PULLUP);
- pinMode(RLED, OUTPUT);
- pinMode(GLED, OUTPUT);
- pinMode(BLED, OUTPUT);
- // Serial.begin(9600);
- // Serial.println("5v"); //button isn't trigger when powered on, so set for 5v in setup
- pinMode(DPlus1, INPUT);
- pinMode(DPlus2, OUTPUT);
- pinMode(DMinus1, INPUT);
- pinMode(DMinus2, INPUT);
- digitalWrite(DPlus2, HIGH);
- digitalWrite(RLED, HIGH); //Write these high to avoid them turning on before vRead can assess
- digitalWrite(GLED, HIGH);
- digitalWrite(BLED, HIGH);
- }
- void loop() {
- if (millis() - previousMillis >= 500) { //check vcc through divider; set LED accordingly
- previousMillis = millis();
- int vRead = analogRead(A0);
- if ((vRead >= 350) && (vRead <= 450)) {
- analogWrite(GLED, 200);
- digitalWrite(BLED, HIGH);
- digitalWrite(RLED, HIGH);
- }
- if ((vRead >= 451) && (vRead <= 750)) {
- digitalWrite(GLED, HIGH);
- digitalWrite(BLED, LOW);
- digitalWrite(RLED, HIGH);
- }
- if ((vRead >= 890) && (vRead <= 970)) {
- digitalWrite(GLED, HIGH);
- digitalWrite(BLED, HIGH);
- digitalWrite(RLED, LOW);
- }
- if (((vRead >= 451) && (vRead <= 750)) && (buttonCount == 2)){ //case where 12v is not supported, but 5v and 9v are
- buttonCount ++;
- }
- }
- if (digitalRead(button) == LOW && millis() - time > debounce) { //Pushbutton for voltage switching
- buttonCount++;
- if (buttonCount >= 4) { //Change based on supply modes available
- buttonCount = 1;
- }
- time = millis();
- }
- if (buttonCount == 1) { //since we set the 5v in setup()
- DPlus_three_v_three(); //9v on first press
- DMinus_sixhundred_mA();
- }
- else if (buttonCount == 2) { //12v
- DPlus_sixhundred_mA();
- DMinus_sixhundred_mA();
- }
- else if (buttonCount == 3) { //5v
- DPlus_sixhundred_mA();
- DMinus_GND();
- }
- }
- //Collection of pin states required for voltage dividers
- void DPlus_sixhundred_mA() {
- pinMode(DPlus1, INPUT);
- pinMode(DPlus2, OUTPUT);
- digitalWrite(DPlus2, HIGH);
- }
- void DPlus_three_v_three() {
- pinMode(DPlus1, OUTPUT);
- pinMode(DPlus2, OUTPUT);
- digitalWrite(DPlus1, HIGH);
- digitalWrite(DPlus2, HIGH);
- }
- void DMinus_GND() {
- pinMode(DMinus1, INPUT);
- pinMode(DMinus2, INPUT);
- }
- void DMinus_sixhundred_mA() {
- pinMode(DMinus1, INPUT);
- pinMode(DMinus2, OUTPUT);
- digitalWrite(DMinus2, HIGH);
- }
- void DMinus_three_v_three() {
- pinMode(DMinus1, OUTPUT);
- pinMode(DMinus2, OUTPUT);
- digitalWrite(DMinus1, HIGH);
- digitalWrite(DMinus2, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement