Some friends were nice enough to give me a Fry's gift card for my birthday. I hate how Fry's is always tempting you in the door with prices that turn out to be based on mail-back rebates that don't always go your way. But this card was a godsend when I needed more components to use to play with my Arduino board. So I got a couple of potentiometers and some LEDs and resistors and I'm in business.
That same evening, I wired up 4 resistors and a potentiometer on the breadboard. I had each led connected to a different digital IO pin -- pins 9-12 -- and the pot is on analog pin 2.
Then I wrote some code that works like this:
- It turns off all the LEDS
- It reads the pot value (range of 0 to 1023)
- If the pot value is more than 0, it lights LED1.
- If the value is more than 1/4 the total, it lights LED2. More than half, light LED3; more than 3/4, light LED4.
The result is a gadget that shows between one and four LEDs lit, depending on how far you dial the potentiometer. That's no big deal, the important part is that I was, that same evening, able to demonstrate using the pot to control something.
Here's the code I used. I'm also sending the pot value back to the computer through the serial link, but I'm doing something wrong there, it's getting random characters....I haven't figured out how to properly convert the int value from the pot into a string. I'm realizing now that the Arduino/Processing language is basically C, and C has lousy built-in string support, and I'm used to scripting languages that are good at strings. Of course, an Arduino doesn't even do floating point arithmetic; it makes sense to keep the code simple and low-level for its purposes.
/* Several LEDS
light, and control, several leds
1. Make 4 leds blink in alternation
2. Read ANY value via serial, from ARD
3. Hook up a pot, read its value
made from Blink without Delay
*
* Turns on and off a light emitting diode(LED) connected to a digital
* pin, without using the delay() function. This means that other code
* can run at the same time without being interrupted by the LED code.
*
* http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
int ledPin1 = 10;
int ledPin2 = 11;
int ledPin3 = 12;
int ledPin4 = 9;
int value = LOW; // previous value of the LED
int oppvalue = HIGH; // opposite value
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
int potPin = 2; // analog pin the pot is on
int potVal = 0; // initial analog value
char potStr[5];
void setup()
{
pinMode(ledPin1, OUTPUT); // sets the digital pin as output
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// Start by turning off all the LEDs
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
potVal = analogRead(potPin); // read the value from the sensor
// this should be btw 0 and 1023
itoa(potVal,potStr,10);
// standard function, takes int, string, and then BASE = base 10 math
Serial.print(potStr);
// Not quite working....returns chars but they aren't numbers like I expected.
if (potVal > 0) {
digitalWrite(ledPin1, HIGH);
}
if (potVal > 255) {
digitalWrite(ledPin2, HIGH);
}
if (potVal > 512) {
digitalWrite(ledPin3, HIGH);
}
if (potVal > 767) {
digitalWrite(ledPin4, HIGH);
}
}