I've been working on a project for about 4 months and I'm starting to wrap it up but I'm having some problems. I have 3 different sets of LEDs that I'm trying to get to flash as well as dim with a potentiometer. I've been successful in a sense but the blinking of the LEDs(rate and intensity) are largely uncontrollable. The code is a mesh of multiple codes with some of my own stuff thrown in. It all works individually but together it's just unsatisfactory. Any help would be appreciated.
Here is the code
int potPin= A0; //Declare potPin to be analog pin A0
int LEDPin[]= {9, 10, 11}; // Declare LEDPin to be arduino pins 6,9,10,11
int LEDState[5]; // last state of each led
int readValue; // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int LEDPin3=3; // set LEDPin3 as output
int LEDPin5=5; // set LEDPin5 as output
int LEDPin6=6; // set LEDPin6 as output
int LEDPin12=12; // LEDPin 13 for Libra
long randNumber;
void setup() {
pinMode (LEDPin12, OUTPUT);
pinMode(potPin, INPUT); //set potPin to be an input
for (int i=0; i<=4; i++){ // set each led pin as an output
pinMode(LEDPin[i], OUTPUT);
}
randomSeed(analogRead(0)); // seed the rnd generator with noise from unused pin
for (int i=0; i<=4; i++){ // init each led with a random value
LEDState[i] = random(20, 185);
}
Serial.begin(9600); // turn on Serial Port
}
void loop() {
digitalWrite(LEDPin12, HIGH);
readValue = analogRead(potPin); //Read the voltage on the Potentiometer
writeValue = (255./1023.) * readValue; //Calculate Write Value for LED
analogWrite(LEDPin3,writeValue); // set the pwm value of pin 3
if (writeValue < 1) {
writeValue = 0;
}
analogWrite(LEDPin5,writeValue); // set the pwm value of pin 5
if (writeValue < 1) {
writeValue = 0;
}
analogWrite(LEDPin6,writeValue); // set the pwm value of pin 6
if (writeValue < 1) {
writeValue = 0;
}
for (int i=0; i<=4; i++){ // for each led:
analogWrite(LEDPin[i],randNumber);
randNumber = random(-20, 45); // generate new rand number add to current value
LEDState[i] += randNumber; // range can be tweaked to change intensity flickering
if (LEDState[i] > 225) { // clamp the limits of the pwm values so it remains within
LEDState[i] = 225; // a pleasing range as well as the pwm range
}
if (LEDState[i] < 10) {
LEDState[i] = 10;
}
}
delay(100); // the delay between changes
Serial.print("You are writing a value of "); //for debugging print your values
Serial.println(LEDState[5]);
}
Im guessing that you might be trying to set the light level of 3 led pins by a pot then setting another 3 to change intensity really fast
im struggling because some of the names are used more than once.
anyway I stuck a few serial prints in the code to see what the arduino thinks its doing and it looks odd
for (int i=0; i<=4; i++){ // for each led:
analogWrite(LEDPin[i],randNumber);
randNumber = random(-20, 45); // generate new rand number add to current value
LEDState[i] += randNumber; // range can be tweaked to change intensity flickering
Serial.print (" random ");
Serial.println ( randNumber);
Serial.print (" ledstate ");
Serial.println ( LEDState[i]);
Serial.print( "pin ");
Serial.println(LEDPin[i]);
serial print reported
random -12
ledstate 213
pin 9
random 41
ledstate 209
pin 10
random 25
ledstate 250
pin 11
random 13
ledstate 171
pin 14
random 20
ledstate 245
pin 8193
this is only 2 long....... 0, 1, 2
nt LEDPin[]= {9, 10, 11};
next "for" is 5 long
for (int i=0; i<=4; i++)
shouldn't that be i<=2
which would print
random 6
ledstate 229
pin 9
random -19
ledstate 206
pin 10
random -8
ledstate 217
pin 11
this text in program seems a odd way to change the intensity but I guess its to slow down the changes
// generate new rand number add to current value
never used random so I shuffled some of your code around and deleted and changed some things
it should give you a better base to start adding your own code.
no idea what this line does but it seems odd you have it on the pot and the text says to have it on a un-used pin. To lazy to look it up
randomSeed(analogRead(0)); // seed the rnd generator with noise from unused pin
int potPin = A0; //Declare potPin to be analog pin A0
int LEDPin[] = {9, 10, 11}; // Declare LEDPin to be arduino pins 6,9,10,11
int LEDState[5]; // last state of each led
int readValue; // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int LEDPin3 = 3; // set LEDPin3 as output
int LEDPin5 = 5; // set LEDPin5 as output
int LEDPin6 = 6; // set LEDPin6 as output
int LEDPin12 = 12; // LEDPin 13 for Libra
byte randNumber;//byte used as overflow is on purpose
void setup() {
pinMode (LEDPin12, OUTPUT);
pinMode(potPin, INPUT); //set potPin to be an input
for (int i = 0; i <= 2; i++) { // set each led pin as an output
pinMode(LEDPin[i], OUTPUT);
}
randomSeed(analogRead(0)); // seed the rnd generator with noise from unused pin
for (int i = 0; i <= 2; i++) { // init each led with a random value
LEDState[i] = random(20, 185);
}
Serial.begin(9600); // turn on Serial Port
}
void loop() {
digitalWrite(LEDPin12, HIGH);
readValue = analogRead(potPin); //Read the voltage on the Potentiometer
writeValue = map(readValue, 0, 1023, 0, 255);
analogWrite(LEDPin3, writeValue); // set the pwm value of pin 3
analogWrite(LEDPin5, writeValue); // set the pwm value of pin 5
analogWrite(LEDPin6, writeValue); // set the pwm value of pin 6
for (int i = 0; i <= 2; i++) { // for each led:
randNumber += random(-20, 40); // generate new rand number add to current value
if (randNumber < 50) {
randNumber = randNumber + 25;//min now 25 but still kinda random
}
LEDState[i] = randNumber; // range can be tweaked to change intensity flickering
analogWrite(LEDPin[i], randNumber);
delay(100); // the delay between changes
}
}
I have had a look at your code. You have quite a few problems in there.
a float in C++ is declared as 255f, not 255. Same with other floats.
This part:
analogWrite(LEDPin3,writeValue); // set the pwm value of pin 3
if (writeValue < 1) {
writeValue = 0;
}
analogWrite(LEDPin5,writeValue); // set the pwm value of pin 5
if (writeValue < 1) {
writeValue = 0;
}
analogWrite(LEDPin6,writeValue); // set the pwm value of pin 6
if (writeValue < 1) {
writeValue = 0;
}
does not do whatever you think it does. It should possibly be this?
if (writeValue < 1) {
writeValue = 0;
}
analogWrite(LEDPin3,writeValue); // set the pwm value of pin 3
analogWrite(LEDPin5,writeValue); // set the pwm value of pin 5
analogWrite(LEDPin6,writeValue); // set the pwm value of pin 6
In setup, you never did this:
pinMode (LEDPin3, OUTPUT);
pinMode (LEDPin5, OUTPUT);
pinMode (LEDPin6, OUTPUT);
This:
analogWrite(LEDPin[i],randNumber);
randNumber = random(-20, 45); // generate new rand number add to current value
LEDState[i] += randNumber; // range can be tweaked to change intensity flickering
if (LEDState[i] > 225) { // clamp the limits of the pwm values so it remains within
LEDState[i] = 225; // a pleasing range as well as the pwm range
}
if (LEDState[i] < 10) {
LEDState[i] = 10;
}
should really be:
randNumber = random(-20, 45); // generate new rand number add to current value
LEDState[i] += randNumber; // range can be tweaked to change intensity flickering
if (LEDState[i] > 225) { // clamp the limits of the pwm values so it remains within
LEDState[i] = 225; // a pleasing range as well as the pwm range
}
if (LEDState[i] < 10) {
LEDState[i] = 10;
}
analogWrite(LEDPin[i],randNumber);
Thank ya'll so much! My silly mistakes have been fixed and the problem has been resolved. im totally in control of the rate and intensity at which the LEDs blink and the range at which they dim has increased too.
Here is the new code with changes that ya'll provided as well as some tweaks I made.
int potPin= A0; //Declare potPin to be analog pin A0
int potPin1=A1; //Declare potPin1 to be analog pin A1
int LEDPin[]= {9, 10, 11};// Declare LEDPin to be arduino pins 8,9,10,11
int LEDState[5]; // last state of each led
int readValue; // Use this variable to read Potentiometer
int writeValue; // Use this variable for writing to LED
int readValue1; // Use this variable to read Libra Potentiometer
int writeValue1; // Use this variable for writing to Libra LED
int LEDPin3=3; // set LEDPin3 as output
int LEDPin5=5; // set LEDPin5 as output
int LEDPin6=6; // set LEDPin6 as output
int LEDPin12=12; // LEDPin 12 for Libra
byte randNumber;// byte used as an overflow
void setup() {
pinMode (LEDPin3, OUTPUT);
pinMode (LEDPin5, OUTPUT);
pinMode (LEDPin6, OUTPUT);
pinMode (LEDPin12, OUTPUT);
pinMode (potPin1, INPUT);
pinMode (potPin, INPUT); //set potPin to be an input
for (int i=0; i<=2; i++){ // set each led pin as an output
pinMode(LEDPin[i], OUTPUT);
}
randomSeed(analogRead(0)); // seed the rnd generator with noise from unused pin
for (int i=0; i<=2; i++){ // init each led with a random value
LEDState[i] = random(50, 185);
}
Serial.begin(9600); // turn on Serial Port
}
void loop() {
readValue1 = analogRead(potPin1); // read potPin1 for Libra
writeValue1 = (readValue1);
if (readValue1 > 10) // set bottom limit to turn L off
digitalWrite(LEDPin12,HIGH); // turn Libra on
else
digitalWrite(LEDPin12,LOW); // turn Libra off
readValue = analogRead(potPin); //Read the voltage on the Potentiometer
writeValue = map(readValue, 0, 1023, 0, 255);
analogWrite(LEDPin3,writeValue); // set the pwm value of pin 3
analogWrite(LEDPin5,writeValue); // set the pwm value of pin 5
analogWrite(LEDPin6,writeValue); // set the pwm value of pin 6
for (int i=0; i<=2; i++){ // for each led:
randNumber = random(-20, 40); // generate new rand number add to current value
if (randNumber < 50) {
randNumber = randNumber + 25; //min 25 but still kinda random
}
LEDState[i] = randNumber; // range can be tweaked to change intensity flickering
if (readValue < 1)
analogWrite (LEDPin[i], readValue);
else
analogWrite(LEDPin[i],randNumber);
delay(100); // the delay between changes
}
Serial.print("You are writing a value of "); //for debugging print your values
Serial.println(writeValue1);
}