A LED doesn't turn on

i was doing this exercise:

"Get the LEDʼs at BOTH ends of the strip to start as on, then to both
move towards each other, appear to bounce off each other and
then move back to the end"

and it went well, but the last led of the left array doesn't turn on and i haven't solve it yet.

ASK_P6

is the 9 pin Led

byte lLed[] = {13, 12, 11, 10, 9};
byte rLed[] = {8, 7, 6, 5, 4};

byte potPin = 0;

byte lAct = 0;
byte rAct = 4;

byte lSum = 1;
byte rSum = -1;

int ledDelay;
byte i;

void setup(){
  
  for(i=0;i<4;i++){ // for del array de lado izq
    
    pinMode(lLed[i], OUTPUT);
  }
  
  for(i=0;i<4;i++){ // for del array del lado der
    
    pinMode(rLed[i], OUTPUT);
  }
}

void loop(){
  
  ledDelay = analogRead(potPin);
  leftLed();
  rightLed();
}

void leftLed(){
  
  for(i=0;i<4;i++){
    
    digitalWrite(lLed[i], LOW);
  }
  
  digitalWrite(lLed[lAct], HIGH);
  delay(ledDelay);
  
  lAct += lSum;
  
  if(lAct == 4){
    lSum = -1;
  } else if(lAct == 0){
    lSum = 1;
  }
}

void rightLed(){
  
  for(i=0;i<4;i++){
    
    digitalWrite(rLed[i], LOW);
  }
  
  digitalWrite(rLed[rAct], HIGH);
  delay(ledDelay);
  
  rAct += rSum;
  
  if(rAct == 4){
    rSum = -1;
  } else if(rAct == 0){
    rSum = 1;
  }
}

How many elements does your array 'rLed' have?

4 elements

Uhm...ok.

How many fingers are there on your right hand? How many on your left hand?

1 Like

i mean 5, my bad jajajja

Ok, so then you can solve it, right?

In case counting gives you trouble, you can also have the Arduino do the counting for you. Consider this:

for (uint8_t i = 0; i < sizeof(array)/sizeof(array[0]); i++)
1 Like

i modified this part

void leftLed(){
  
  for(i=0;i<4;i++){
    
    digitalWrite(lLed[i], LOW);
  }
  
  digitalWrite(lLed[4], HIGH);
  
  /* digitalWrite(lLed[lAct], HIGH);
  delay(ledDelay);
  
  lAct += lSum;
  
  if(lAct == 4){
    lSum = -1;
  } else if(lAct == 0){
    lSum = 1;
  } */
}

ASK_P6

and now the lLed[4] (the trouble led) turned on, but its intensity is so low.
i'm a little bit confused bc the right side array of leds worked well and i wanna believe it's the same.

on the other hand, i just found that in the original code, the led in fact, does lights up, but at low intensity compared with the rest, and it stay still...

btw, sorry for my bad english

That still only will work on 4 elements of an array which you just counted as having 5 elements.

Usually this is due to a pin not being set as OUTPUT. Which makes sense because your code never works on the last pin due to what I said above.

Again, this suggestion will likely solve your issue once and for all:

I FINALLY GET IT! it now works well. thank u sm! :slight_smile:
it's a shame i couldn't understand what were you saying at first and that i didn't realize the mistake in the for statement. anyway... goodnight and greetings from mx

ASK_P6

Hehe :slight_smile: Congrats on solving the problem.

1 Like

Hello abrilxcx
I´ve found a "Knight Rider" sketch in my sketch box for used sketches. Try and check it. Put this sketch in your box with the name "how to use structured arrays very easy".

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  Many thanks to LarryD
  https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  https://forum.arduino.cc/t/a-led-doesnt-turn-on/970981
  Tested with Arduino: Mega[ ] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Knight Rider "
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte PotPin {A5};
constexpr byte LedPins[] {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // portPin o---|220|---|LED|---GND
struct LEDPATTERN {
  byte content[10];
};
LEDPATTERN ledPattern[] {
  {false, false, false, false, false, false, false, false, false, false},
  {false, true, false, false, false, false, false, false, true, false},
  {false, false, true, false, false, false, false, true, false, false},
  {false, false, false, true, false, false, true, false, false, false},
  {false, false, false, false, true, true, false, false, false, false},
  {false, false, false, true, false, false, true, false, false, false},
  {false, false, true, false, false, false, false, true, false, false},
  {false, true, false, false, false, false, false, false, true, false},
  {true, false, false, false, false, false, false, false, false, true},
};
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  //  https://www.learncpp.com/cpp-tutorial/for-each-loops/
  for (auto Output_ : LedPins) pinMode(Output_, OUTPUT);
}
void loop () {
  unsigned long currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  static unsigned long myMillis;
  if (currentTime - myMillis >= (unsigned long) map(analogRead(PotPin), 0, 1023, 10, 200)) {
    myMillis = currentTime;
    static int patternCounter {0};
    int element {0};
    for (auto LedPin : LedPins) digitalWrite(LedPin, ledPattern[patternCounter].content[element++]);
    patternCounter = (patternCounter + 1) % (sizeof(ledPattern) / sizeof(ledPattern[0]));
  }
}
Have a nice day and enjoy coding in C++.
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.