Saturday, October 1, 2011

using two pots to influence led behavior

i wanted to try something independent from the given examples and came up with the following (trying to understand that everything is a building block). so i wanted one physical input that controls the time my led is on. and one physical input that controls the time my led is off. my two rotary potentiometers served me well for this. in order to see on my computer screen what values the pots return, i plotted their values into the serial monitor. i can now run the sketch on the arduino and adjust the frequency and on-vs-off-time-ratio with the two pots. see movie below to see what i mean.


Here's the code:

int sensorPin1 = A0; // select the input pin for the potentiometer one
int sensorPin2 = A1; // select the input pin for the potentiometer two
int ledPin = 9; // select the pin for the LED
int sensorValue1 = 0; // variable to store the value coming from pot1
int sensorValue2 = 0; // variable to store the value coming from pot2

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT:
Serial.begin(9600); // start serial com to monitor values visually as i change pots

}

void loop() {
// read the value from the sensors:
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
// turn the ledPin on as per value of pot1
digitalWrite(ledPin, HIGH);
// stop the program for
milliseconds:
delay(sensorValue1);
// turn the ledPin off as per vaalue of pot2:
digitalWrite(ledPin, LOW);
delay(sensorValue2);

Serial.println(sensorValue1); // output the value to see what it is in the monitor
Serial.println(sensorValue2); // output the value to see what it is in the monitor
}


it took me a while to realize this, and it was an eye opener:
- your inputs and outputs are not physically connected, just through the software
- the code is a bit confusing because it uses variables to camouflage what it's doing kind of
- the two blue pots send their signal as a voltage input to the program (follow red arrow)
- the outputs send the corresponding information to the led as an output (follow green arrow)
- in reality, the code runs on the arduino, not on your pc (so the arrows stay on the board), but it's easy to visualize it that way



No comments:

Post a Comment