Skip to content

Instantly share code, notes, and snippets.

@bangonkali
Last active December 30, 2015 16:19
Show Gist options
  • Select an option

  • Save bangonkali/7853547 to your computer and use it in GitHub Desktop.

Select an option

Save bangonkali/7853547 to your computer and use it in GitHub Desktop.
int sensorPin = A0;
int timer = 0; // The higher the number, the slower the timing.
int ledPins1[] = {6,7,8,9}; // an array of pin numbers to which LEDs are attached
int ledPins2[] = {10,11,12,13};
int pinCount = 4; // the number of pins (i.e. the length of the array)
int state = 0;
const int buttonPin = 2;
const long doubleClickThreshold = 200;
const long debounceThreshold = 60;
unsigned long currentTime;
volatile unsigned long prevTime;
volatile int prevState = 0;
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
pinMode(buttonPin, INPUT);
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins1[thisPin], OUTPUT);
pinMode(ledPins2[thisPin], OUTPUT);
}
attachInterrupt(0, blink, RISING );
}
void blink()
{
currentTime = millis();
if ((currentTime - prevTime) > debounceThreshold) {
if ((currentTime - prevTime) <= doubleClickThreshold) {
prevState = 3;
} else {
if (prevState == 1) {
prevState = 0;
} else if (prevState == 0) {
prevState = 1;
} else if (prevState == 3) {
prevState = 0;
}
}
}
prevTime = currentTime;
}
void loop() {
timer = analogRead(sensorPin);
if (prevState == 1) {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins1[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins1[thisPin], LOW);
}
delay(timer);
}
else if (prevState == 0) {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins2[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins2[thisPin], LOW);
}
delay(timer);
} else if (prevState == 3) {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins1[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins1[thisPin], LOW);
}
delay(timer);
// loop from the highest pin to the lowest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin off:
digitalWrite(ledPins2[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins2[thisPin], LOW);
}
delay(timer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment