Go Down

Topic: Lights not working as planned (SOLVED) (Read 737 times) previous topic - next topic

CormAlan

Ok, I've attached an image of the circuit I'm using.

CormAlan

This is the code that ONLY makes the LEDs work
(I didn't make it originally, its from the book "Arduino projects for dummies", which I'm learning from)

Code: [Select]

// A variable to set a delay time between each LED
int delayTime = 40;

// A variable to store which LED we are currently working on
int currentLED = 4;

// A variable to store the direction of travel of LEDs
int dir = 1;

// A variable to store the last time we changed something
unsigned long timeChanged = 0;

// Create an array to hold the value for each LED pin
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

void setup() {
  // Set all pins for OUTPUT
  for (int x=0; x<10; x++) {
    pinMode(ledPin[x], OUTPUT);
  }

 // Set up the
  timeChanged = millis();
}

void loop() {
  // Check whether it has been long enough
  if ((millis() - timeChanged) > delayTime) {

    // Turn off all of the LEDs
    for (int x=0; x<10; x++) {
      digitalWrite(ledPin[x], LOW);
    }

    // Turn on the current LED
    digitalWrite(ledPin[currentLED], HIGH);

    // Increment by the direction value
    currentLED += dir;

    // If we are at the end of a row, change direction
    if (currentLED == 9) {
      dir = -1;
    }
    if (currentLED == 0) {
      dir = 1;
    }
  

  // Store the current time as the time we last changed LEDs
  timeChanged = millis();
  }
}



wildbill

Can you also post the code that's failing?

CormAlan

This is the code I'm trying to add the number generator to, which is failing
Code: [Select]

// Random number picker
// Sequentially lights up leds

// Include entropy
#include <Entropy.h>

// A variable to set a time between LEDs in milliseconds
int delayTime = 100;

// A variable to choose the LED
int currentLED = 4;

// Choosing the direction of travel
int dir = 1;

// A variable to say how long it's been since we changed something
long timeChanged = 0;

// An array to hold the value for each LED pin
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

void setup() {

  for (int i=0; i<10; i++) {
    pinMode(ledPin[i], OUTPUT);  //set all pins for OUTPUT
    digitalWrite(ledPin[i], LOW);
  }
  int rannumber = Entropy.random(11);
  timeChanged = millis(); 
}

void loop() { 
 
  if ((millis() - timeChanged) > delayTime) {   // Check wether it has been long enough
    timeChanged = millis();  // Store the current time as we last changed LEDs
    for (int i=0; i<10; i++) {
     digitalWrite(ledPin[i], LOW);
    }   
    digitalWrite(ledPin[currentLED], HIGH);  //turn it on
    currentLED += dir;
 
          // If we are at the end of a row, change direction
    if (currentLED == 9) {
      dir = -1;
      }
 
    if (currentLED == 0) {
      dir = 1;
    }
  }
}



UKHeliBob

I have lost track of the problem.

In the code you posted you appear to generate a random number only once in setup() and never use it

Can you please describe the problem that you have with the code ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

wildbill

I pulled the entropy library from here: https://github.com/pmjdebruijn/Arduino-Entropy-Library. Is that the one you're using?

I put in some serial.print statements and ran it on an Uno.

It crashes at the entropy.random call. If you comment it out, you actually get to loop.

wildbill

Oh, and looking at the examples for the library, there's a predecessor call for the entropy library that you need prior to the call to the random method:

Code: [Select]

  Entropy.initialize();


That fixed the crash for me.

CormAlan

#37
Jan 12, 2019, 04:46 pm Last Edit: Jan 12, 2019, 04:48 pm by CormAlan
Oh- yeah, I forgot to add the
Code: [Select]

if (currentLED == rannumber) {
delay = 9999999;
}


I'll add that, but the other problem isn't a compiling error per se. It compiles fine, but the only thing the Arduino does after compilation is that the led on pin 13 flashes.
Including the code using the rannumber, here is the current code:

Code: [Select]

// Random number picker
// Sequentially lights up leds

// Include entropy
#include <Entropy.h>

// A variable to set a time between LEDs in milliseconds
int delayTime = 100;

// A variable to choose the LED
int currentLED = 4;

// Choosing the direction of travel
int dir = 1;

// A variable to say how long it's been since we changed something
long timeChanged = 0;

// An array to hold the value for each LED pin
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

void setup() {

  for (int i=0; i<10; i++) {
    pinMode(ledPin[i], OUTPUT);  //set all pins for OUTPUT
    digitalWrite(ledPin[i], LOW);
  }
  int rannumber = Entropy.random(11);
  timeChanged = millis(); 
}

void loop() { 
 
  if ((millis() - timeChanged) > delayTime) {   // Check wether it has been long enough
    timeChanged = millis();  // Store the current time as we last changed LEDs
    for (int i=0; i<10; i++) {
     digitalWrite(ledPin[i], LOW);
    }   
    digitalWrite(ledPin[currentLED], HIGH);  //turn it on
    currentLED += dir;
 
          // If we are at the end of a row, change direction
    if (currentLED == 9) {
      dir = -1;
      }
 
    if (currentLED == 0) {
      dir = 1;
    }
  if (currentLED == rannumber) {
    delay = 999999999;
  }
    }
  }
}


The problem is that "rannumber" is not declared in void loop(), but I don't know how to declare it once in void loop without it happening every loop.
Also, what are the serial statements meant to be?

CormAlan

Wait a second!
After I added the Entropy.initialize();, the lights started to move back and fourth, as intended!
The only thing to add now might pose a problem though. I removed the thing that sets the delay to 99999999 after it lands on the random number, but I want to add it into the loop again. I'm not able to do this though, because the rannumber variable is now declared in the loop. How do I use the variable rannumber in the loop without it being declared every loop?

UKHeliBob

Quote
The problem is that "rannumber" is not declared in void loop(), but I don't know how to declare it once in void loop without it happening every loop.
Either declare it as global or declare it as static in loop() which will effectively stop its value being changed by the declaration when it is declared again next time through loop()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

CormAlan

I was so sure this would work but its still got one last problem.
I made rannumber a static variable in loop(), and added this:
Code: [Select]

if (currentLED == rannumber) {
   delay = 99999999;
}


but the line that sets delay to 999999999 is highlighted in red, and the error message reads:

exit status 1
assignment of function 'void delay(long unsigned int)'

what has gone wrong?

wildbill

You probably meant delayTime = 999999;

You will need to change delayTime to unsigned long rather than int too.

CormAlan

#42
Jan 12, 2019, 05:14 pm Last Edit: Jan 12, 2019, 05:23 pm by CormAlan
YES!!!!
It's working perfectly! Huge thanks (and karma) to everyone who helped, no idea on how to set this thread to solved though- couldn't have done it without you! :)
Also- I was successfully able to add the bounces(); thing, so that it only happens after 5 bounces!


ps wow this was a long thread

UKHeliBob

Do you really want to stop anything happening for 99999999 milliseconds ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

wildbill

no idea on how to set this thread to solved though
You should be able to edit the thread title & add the word solved

Go Up