Arduino - 4 LED's blinking issue

Thread Starter

spuddo

Joined May 2, 2013
76
Hi
In the attached sketch i hoped to achieve 4 LED's blinking at a rate of 1Hz to 20Hz , each controlled by a pot.
It uploaded , but all LED's were on and blinking at 2.6KHz with no control by each pot.
Any thoughts.

Code:
int Ledpin[] =  {8, 9, 10, 11};
int Potpin[] =  {A0, A1, A2, A3};
boolean state[4];

void setup() {
  for (int i; i < 4; i++)
  { pinMode(Ledpin[i], OUTPUT);
    pinMode(Potpin[i], INPUT);
    state[i] = LOW;
  }
}
void loop() {

  int potValue = analogRead(Potpin);
  int Potpin = map (potValue , 0, 1023, 500 , 25); // 1Hz to 20Hz

  for (int i; i < 4; i++)
  { if (digitalRead(Potpin);

        state[i] = !state[i]);
    digitalWrite(Ledpin[i], state[i]);
  }
}
 
Last edited:

sagor

Joined Mar 10, 2019
903
Why are you doing a digital read of the Potpin?
I see no timer/delay loop, so the code will simply run very fast all the time.
Potpin is defined as an array, but you don't use array index for that variable, except once for Potpin[1].
 

JavaLi

Joined Jun 24, 2017
8
And the same name for global and local variable!
The line of "if (digitalRead(Potpin);" do nothing!!! Missing braces and code for decision!!!
if (condiction){
// TODO something here
}
 
Last edited:

Thread Starter

spuddo

Joined May 2, 2013
76
Hi , sagor and JavLi , thanks for your replies.

Both of you have pointed out the same error.
Could you direct me to the right tutorial to explain this issue.
I have probably read too much in the last week or so , and i don' know whether i'm Arthur or Martha.
I'm new to this and getting on a bit , so a little help goes a long way.
Best regards.

Why are you doing a digital read of the Potpin?
I see no timer/delay loop, so the code will simply run very fast all the time.
Potpin is defined as an array, but you don't use array index for that variable, except once for Potpin[1].
And the same name for global and local variable!
The line of "if (digitalRead(Potpin);" do nothing!!! Missing braces and code for decision!!!
if (condiction){
// TODO something here
}
 

JavaLi

Joined Jun 24, 2017
8
I didn't understand so much what you wanted to do, but, checkout this example:

Code:
int Ledpin[] =  {6, 9, 10, 11}; // Arduino PWM Pins
int Potpin[] =  {0, 1, 2, 3};

void setup() {
  for (int i = 0; i < 4; i++){
      pinMode(Ledpin[i], OUTPUT);
      pinMode(Potpin[i], INPUT);
  }
}

void loop() {
  long val;
   for (int i = 0; i < 4; i++){
        val = analogRead(Potpin[i]);
        val = map (val , 0, 1023, 500 , 25);
        analogWrite(Ledpin[i], val);  
  }  
}
 

Attachments

Thread Starter

spuddo

Joined May 2, 2013
76
Hi JaVali , thanks for your reply.
I started out with this code.

const int ledPin = 11;
const int Potpin = A0;
int ledState = LOW;
unsigned long previousMillis = 0;

void setup() {

pinMode(ledPin, OUTPUT);
}

void loop() {

int potValue = analogRead(A0);
int Potpin = map (potValue , 0, 1023, 500 , 25);

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= Potpin) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

digitalWrite(ledPin, ledState);
}
}

This gave me one pot controlling the blink rate of 1Hz to 20Hz.
I wanted to expand this to 4 pots each independently controlling an LED at the above rate.
Hence my attempt to achieve this----it failed.
Here's another attempt with a "close but no cigar" outcome.
All 4 LED's blinked at the same (1-20Hz) rate , but any pot would have the same effect , therefore not independent of each other.
Getting closer.
Any thoughts?

Code:
#define SETS 4
const int LEDpins[SETS] = { 8, 9, 10, 11 };
const int potPins[SETS] = { A0, A1, A2, A3 };


// each "event" (LED) gets their own tracking variable
unsigned long previousMillis[SETS];

// different intervals for each LED
unsigned int interval[SETS];

void setup() {
  for ( int i = 0; i < SETS; ++i ) {
    pinMode( LEDpins[i], OUTPUT );
  }
}

void loop() {

  unsigned long currentMillis = millis();

  for ( int i = 0; i < SETS; ++i ) {
    int potValue = analogRead( potPins[i]);
    interval[i] = map(potValue, 0, 1023, 500, 25);

    if (currentMillis - previousMillis[i] >= interval[i]) {
      digitalWrite(LEDpins[i], !digitalRead(LEDpins[i]));
      previousMillis[i] = currentMillis;
    }
  }
}
 
Top