Skip to content

Instantly share code, notes, and snippets.

@erubboli
Created September 18, 2011 10:30
Show Gist options
  • Save erubboli/1224956 to your computer and use it in GitHub Desktop.
Save erubboli/1224956 to your computer and use it in GitHub Desktop.
arduino test #2
#include "AnalogButtons.h"
int led_pin = 13;
int res_pin = A0;
int interrupt_pin = 0;
volatile int msec = 1000;
#define MODE_EDIT 0
#define MODE_RUN 1
int mode = MODE_EDIT;
AnalogButtons analogButtons(res_pin, 30, &handleButtons);
Button b1 = Button(1, 770, 775);
Button b2 = Button(2, 820, 825);
Button b3 = Button(3, 850, 860);
void setup() {
Serial.begin(9600);
pinMode(led_pin, OUTPUT);
turn_off();
attachInterrupt(interrupt_pin, switch_to_edit, CHANGE);
analogButtons.addButton(b1);
analogButtons.addButton(b2);
analogButtons.addButton(b3);
}
void loop() {
if(mode == MODE_EDIT){
analogButtons.checkButtons();
}else if (mode == MODE_RUN){
turn_on();
delay(msec);
turn_off();
delay(msec);
}
}
void switch_to_edit(){
turn_off();
mode = MODE_EDIT;
}
void turn_off(){
Serial.println("Turn off LED");
digitalWrite(led_pin, HIGH);
}
void turn_on(){
Serial.println("Turn on LED");
digitalWrite(led_pin, LOW);
}
void handleButtons(int id, boolean held){
if (held) {
Serial.print("button id="); Serial.print(id); Serial.println(" was pressed and held");
}else{
Serial.print("button id="); Serial.print(id); Serial.println(" was pressed only");
if(id==1){
mode=MODE_RUN;
}else if (id == 2){
msec = msec + 100;
Serial.print("new msec="); Serial.println(msec);
}else if (id == 3 && msec > 100){
msec = msec - 100;
Serial.print("new msec="); Serial.println(msec);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment