Skip to content

Instantly share code, notes, and snippets.

@branliu0
Created August 1, 2012 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branliu0/3229691 to your computer and use it in GitHub Desktop.
Save branliu0/3229691 to your computer and use it in GitHub Desktop.
/*
http://arduino.cc/en/Tutorial/AnalogInput
*/
#define SENSOR_PIN A0 // select the input pin for the potentiometer
#define NUM_PINS 8
#define START_PIN 1
int pin_interval;
void setup() {
pin_interval = 1024 / NUM_PINS;
// declare the pins as an OUTPUT:
for(int i = START_PIN; i <= START_PIN + NUM_PINS; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
lightSpeed();
}
void oneLight() {
// Value between 0 and 1023
int sensor_value = analogRead(SENSOR_PIN);
int pin = sensor_value/pin_interval + START_PIN;
for (int i = START_PIN; i <= START_PIN + NUM_PINS; i++) {
if (i == pin) {
digitalWrite(i, HIGH);
} else {
digitalWrite(i, LOW);
}
}
delay(10);
}
void lightBar() {
// Value between 0 and 1023
int sensor_value = analogRead(SENSOR_PIN);
int pin = sensor_value/pin_interval + START_PIN;
for (int i = START_PIN; i <= START_PIN + NUM_PINS; i++) {
if (i <= pin) {
digitalWrite(i, HIGH);
} else {
digitalWrite(i, LOW);
}
}
delay(10);
}
int current_pin = START_PIN;
void lightSpeed() {
int next_pin = ((current_pin - START_PIN + 1) % NUM_PINS) + START_PIN;
for (int i = START_PIN; i <= START_PIN + NUM_PINS; i++) {
if (i == next_pin) {
digitalWrite(i, HIGH);
} else {
digitalWrite(i, LOW);
}
}
current_pin = next_pin;
int sensor_value = analogRead(SENSOR_PIN);
delay(sensor_value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment