Program a potentiometer
Recommended: refer to the wiring diagram for potentiometer on Arduino playground
Alternatively refer to this diagram:
This program will allow adjust the speed of a blinking LED with analog input. Follow this patch to connect a potentiometer to your Arduino board. Then put an the long lead of an LED in pin 13 of your digital in/out, and the short lead in Ground.

Download the diagram.
potentiometer.pdf
/*
* AnalogInput
* by DojoDave <http://www.0j0.org>
*
* Turns on and off a light emitting diode(LED) connected to digital
* pin 13. The amount of time the LED will be on and off depends on
* the value obtained by analogRead(). In the easiest case we connect
* a potentiometer to analog pin 2.
*/
int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}
Program a flex sensor
A Flex sensor changes resistance between 10k Ohms (straight) and 40k Ohms (bent). Follow this schematic to patch a flex sensor to your Arduino board. Connect the long lead of an LED in pin 13 of your digital in/out, and the short lead in Ground.
Download the schematic and breadboard diagram.
Copy and paste the following code into an Arduino sketch:
int ledPin = 13; // declares a variable for the pin for an LED
int analoginPot = 2; // declares a variable for the analog input pin
int potval = 0; // declares a variable for reading the pin status
unsigned long ledCounter = 0; // a special integer variable for really big numbers
int toggle = 0; // declares a variable for a light switch, sets value to either 1 or 0
void setup() {
pinMode(ledPin, OUTPUT); // declares pin 13 as the output
Serial.begin(9600); // necessary if we want to report back to screen later in the script
}
void loop(){
potval = analogRead(analoginPot); // reads input value of analog pin 2
Serial.println(potval);
potval = (potval - 764); // bend your flex sensor with serial monitor ON, subtract the lowest value received by your sensor
ledCounter ++; // adds 1 to our variable, represents time btwn on and off
if (ledCounter > (potval * 10)) { // multiplies potval variable times 10
// if condition is met it does the following...
ledCounter = 0; // resets counter
Serial.println("blink"); // prints the word “blink” in the Serial monitor
if (toggle == 0){ // checks if light is switched on or off
digitalWrite(ledPin, HIGH); // light (LED) turns on
toggle = 1; // changes the switch for next time, so that the next time through it sets toggle to off
}
else if (toggle == 1){ // if toggle value is 1 and not 0 then ...
digitalWrite(ledPin, LOW); // turns light on
toggle = 0; // changes the switch for next time through
}
}
Comments (0)
You don't have permission to comment on this page.