Skip to content

Instantly share code, notes, and snippets.

@adlerweb adlerweb/vref.ino
Created Jul 28, 2017

Embed
What would you like to do?
Voltage reference hack
#include <Bounce2.h>
//Pin mapping
const byte LED[] = {2,4,3,5}; //Jepp, I screwed up while soldering ;)
const byte SW = 6;
const byte SYSLED = LED_BUILTIN;
//ATT: When powering on or switching voltages the output may provide +10V for a brief moment
const String volt[] = {
"2.5",
"5.0",
"7.5",
"10.0"
};
const byte OUT=4;
const byte BUILD=1;
const unsigned int BEAT=1000; //Blink LED and send UART status every 1000ms
byte active = 0;
boolean pressed = false;
unsigned long next = 0;
Bounce debouncer = Bounce();
void setup() {
// put your setup code here, to run once:
pinMode(SW, INPUT_PULLUP);
pinMode(SYSLED, OUTPUT);
debouncer.attach(SW);
debouncer.interval(5); // interval in ms
for(byte i=0; i<OUT; i++) {
pinMode(LED[i], OUTPUT);
digitalWrite(LED[i], LOW);
}
digitalWrite(LED[active], HIGH);
Serial.begin(9600);
Serial.print(F("Started voltage reference build "));
Serial.println(BUILD);
Serial.println(F("---"));
Serial.println(F("Available Settings:"));
for(byte i=0; i<OUT; i++) {
Serial.print(i);
Serial.print(F(" -> "));
Serial.print(volt[i]);
Serial.println('V');
}
Serial.println(F("---"));
}
void loop() {
byte i;
byte na = active;
debouncer.update();
if(debouncer.read() == LOW) {
if(!pressed) {
pressed = true;
na++;
if(na >= OUT) na = 0;
}
}else{
pressed = false;
}
if(Serial.available() > 0) {
i = Serial.read();
if(i >= 0x30 && i < (0x30+OUT)) {
na = (i-0x30);
}else if(i != 0x0A && i != 0x0D) { //Ignore newline
Serial.print(F("Invalid input: 0x"));
Serial.println(i, HEX);
}
}
if(na != active) {
active = na;
for(i=0; i<OUT; i++) digitalWrite(LED[i], LOW);
digitalWrite(LED[active], HIGH);
Serial.print(F("Switched to "));
Serial.print(volt[active]);
Serial.println('V');
next = millis() + BEAT;
}
if(next <= millis() || millis() < (next - BEAT)) {
next = millis() + BEAT;
Serial.print("Currently at ");
Serial.print(volt[active]);
Serial.println('V');
digitalWrite(SYSLED, !digitalRead(SYSLED));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.