Multiple independent LED speed control code via multiple independent potentiometers.
I imagine this to be pretty simple to do.
I only really need to do this with 4 independent outputs and controls.
I was hoping I could do this with 4 analog inputs and 4 digital outputs.
So far I have been using this code for 1 circuit:
int potPin0 = 0; // select the input pin for the potentiometer
int ledPin0 = 0;
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin0, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin0); // read the value from the sensor
digitalWrite(ledPin0, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin0, LOW); // turn the ledPin off
delay(val); // stop the program for some time
// stop the program for some time
}
I am still very new to this and need help with the code. I was hoping i could just do this:
int potPin0 = 0; // select the input pin for the potentiometer
int ledPin0 = 0;
int val = 0; // variable to store the value coming from the sensor
int potPin1 = 1;
int ledPin1 = 1;
void setup() {
pinMode(ledPin0, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(ledPin1, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin0); // read the value from the sensor
digitalWrite(ledPin0, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin0, LOW); // turn the ledPin off
delay(val); // stop the program for some time
val = analogRead(potPin0); // read the value from the sensor
digitalWrite(ledPin1, HIGH); // turn the ledPin on
delay(val) // stop the program for some time
digitalWrite(ledPin1, LOW); // turn the ledPin off
delay(val) // stop the program for some time
}
But I get problems once I start adding additional "val" commands.
Anyone have any suggestions? Should I go about this differently?
Thanks very much in advance,
-Kevin
"But I get problems once I start adding additional "val" commands."
What kind of problems? You only seem to be reading 1 analog input - did you intend to read 2?
"val" can be 0 to 1023. So once you read, you are not checking anything else for 2 x val, or up to 2 seconds.
Did you want the program to be more responsive? If so, you need to use Blink-without-delay type programming, where you note the time, start the action, and check the time every subsequent pass thru void loop to determine if is time to stop the action.
int potPin0 = 0; // select the input pin for the potentiometer
int ledPin0 = 0;
int val = 0; // variable to store the value coming from the sensor
int potPin1 = 1;
int ledPin1 = 1;
void setup() {
pinMode(ledPin0, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(ledPin1, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = analogRead(potPin0); // read the value from the sensor
digitalWrite(ledPin0, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin0, LOW); // turn the ledPin off
delay(val); // stop the program for some time
val = analogRead(potPin1); // read the value from the sensor
digitalWrite(ledPin1, HIGH); // turn the ledPin on
delay(val) // stop the program for some time
digitalWrite(ledPin1, LOW); // turn the ledPin off
delay(val) // stop the program for some time
}
I was experimenting with the code still..
I would like to have 4 digital outputs turn on and off and have the speed of each controlled by 4 separate analog read/inputs.. via 4 potentiometers.
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning 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().
The circuit:
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground
* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.
Created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
and alter it, I might be able to have independent multiple speed controls for different pins?
Ok, I have it one potentiometer controlling 4 digital outputs:
/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning 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().
The circuit:
* Potentiometer attached to analog input 0
* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground
* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.
Created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 0; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int sensorPin1 = A1;
int ledPin1 = 1;
int sensorPin2 = A2;
int ledPin2 = 2;
int sensorPin3 = A3;
int ledPin3 = 3;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
Any thoughts on how to have each output read from a different analog signal / sensorPin?