Interactive LED chase effect

Hi,

I’m trying to run a program that is supposed to do an interactive LED chase effect but when I am verifying in the arduino IDE I get this error : exit status 1
‘changeLED’ was not declared in this scope

Here’s my code :

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay;

int direction = 1;
int currentLed = 0;
unsigned long changeTime;
int potPin = 2;


void setup() {
for (int x=0; x<10; x++)
{ 

pinMode(ledPin[x], OUTPUT);
}
changeTime = millis();
}

void loop() {
ledDelay = analogRead(potPin);
if ((millis() - changeTime)
> ledDelay) {
 changeLED ();
 changeTime =
 millis();
 
}

}

void changeLED () [
for (int x=0; x<10; x++)
{
 digitalWrite (ledPin[currentLED], HIGH);
 currentLED += direction;
 if (currentLED == 9)
 {direction = -1;}
 if (currentLED == 0)
 {direction = 1;}
}

I’m wondering if I am supposed to write the ‘‘void changeLED’’ before the ‘‘void loop’’ ?

any ideas?

Please use code tags (</> in the toolbar) to make your code readable. You can edit your original post to add this in.

void changeLED () [

what is the ‘[’?

we can't read your code... so we can't help

Looks like I messed up. Just edited, it looks way better lol.

marco_c:
Please use code tags (</> in the toolbar) to make your code readable. You can edit your original post to add this in.

void changeLED () [

what is the ‘[’?

Thank you I just revised my code and you found out my error. It’s amazing how a wrong character can messed things up.

It’s amazing how a wrong character can messed things up.

Not really. You are describing to the compiler what the program should do so need to be very precise.

so the compiler says that my code have no errors and that's great. But when I'm trying to run it on arduino, le 9th LED stays HIGH and nothing is happening.

I tried without the potentiometer and this is working just fine but when I'm adding the interactive effect it don't.

Could it be because I'm using a 50K ohms potentiometer instead of the 4.7 K recommended by the book ?

Your pot is fine.

Assuming your code has not changed (as you have not posted new code) the problem is likely to be with this code:

 digitalWrite (ledPin[currentLED], HIGH);
 currentLED += direction;
 if (currentLED == 9)
 {direction = -1;}
 if (currentLED == 0)
 {direction = 1;}

I don't see where you turn any of the LEDs off? They should in fact all be on after the first run through.

For your information, I took this code from the book Beginning Arduino (technology in action) by Michael McRoberts on kindle.

So here’s my code, I don’t think it has changed since my first post but tried to remove a LED once so maybe I messed something up but can’t quite figure what it is.

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

int ledDelay;

int direction = 1;
int currentLed = 0;
unsigned long changeTime;
int potPin = 2;
void setup()
{
	for (int x=0; x<10; x++)
{
	pinMode (ledPin[x], OUTPUT);
}
changeTime = millis();	
}

void loop()
{
ledDelay = analogRead (potPin);	
if ((millis() - changeTime) > ledDelay) {
	changeLed();
	changeTime = millis();
	}
}

void changeLed() {
	for (int x=0; x<10; x++)
	{
		digitalWrite (ledPin[x], LOW);
		}
		digitalWrite (ledPin[currentLed], HIGH);
		currentLed =+ direction;
		
		if (currentLed == 9);
		{direction = -1;}
		
	if (currentLed == 0);
	{direction = 1;}
}

In the book, it has // between the lines of the code and it says :

void changeLed() {
	for (int x=0; x<10; x++)
	{
//turn off all LED
		digitalWrite (ledPin[x], LOW);
		}
		digitalWrite (ledPin[currentLed], HIGH);
		currentLed =+ direction;

so I think this is where the LED gets turn off.

Also, the problem seems to come from the potentiometer or the code that include it since when I’m running the code without it it is working just fine. Here’s the code without the potentiometer :

// LED Chase Effect

byte ledPin [] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// Create array for LED pins

int ledDelay = 80;
// delay between changes

int direction = 1;
int currentLed = 0;
unsigned long changeTime;

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

void loop()
{
if ((millis() - changeTime) > ledDelay) {
// if it has been ledDelay since last change
changeLed();
changeTime = millis();
}	
}

void changeLed () {
for (int x=0; x<10; x++) {
	digitalWrite(ledPin[x], LOW);
	// turn off all leds
}
digitalWrite(ledPin[currentLed], HIGH);
// turn on the current leds
currentLed += direction;
// increment by the direction value
if (currentLed ==9) {direction = -1;}
if (currentLed ==0) {direction =1;}
// change direction if we reach the end
}

The code is different. For a start you are turning LEDs off.

Is potpin pin 2 or pin A2?

potPin is in the analog pin number 2.

enjoythedecline: potPin is in the analog pin number 2.

That's not what the code says.

That's not what the code says.

Oh yes it does. analogRead() only works with analogue inputs and 2 is interpreted in this context as analogue pin 2, aka A2

Whilst the A* format is more explicit both forms work,

so what should I do ?

Well, if you are sure that 2 is the same as A2 (have you tried changing this?) then the problem could be in the way you have wired up the potentiometer. Post a good (in focus) photo of your setup or describe it.

I had a close look at what you had done and set up some hardware to test your stuff. At the bottom is code that works with the pot. What was incorrect in your code:

if (currentLed == 0);
	{direction = 1;}

The ; after the ) finishes the if statement there, so the next statement is always executed. You did this for the other if statement as well.

currentLed =+ direction;

The correct syntax is for ‘currentLed = currentLed + direction’ is += not =+. =+ is the statement ‘currentLed = +direction’.

If you are copying code from magazines/books, you need to make sure that you copy it as it is, and check that you have properly copied it. Neither of these are incorrect statements but they are not what you intended. The computer will execute what you say, not what you mean.

Improvements:

  • Don’t use magic numbers in the code like ‘10’ for the size of the array. It makes code maintenance ambiguous (which 10 is the 10 you need to change?), especially when you start writing longer programs. The macro defined as ARRAY_SIZE allows you to just maintain the ledPin array and the code automatically adjusts to the new size. You could also have defined the size as a constant and used that as the array size between . The compiler will then create an error if the data initialisers and define don’t match.

Working code:

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

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

int ledDelay;
int direction = 1;
int currentLed = 0;
unsigned long changeTime;
int potPin = 2;

void setup()
{
  for (int x = 0; x < ARRAY_SIZE(ledPin); x++)
    pinMode (ledPin[x], OUTPUT);

  changeTime = millis();
}

void loop()
{
  ledDelay = analogRead(potPin);
  if ((millis() - changeTime) > ledDelay) 
  {
    changeLed();
    changeTime = millis();
  }
}

void changeLed() 
{
  for (int x = 0; x < ARRAY_SIZE(ledPin); x++)
    digitalWrite (ledPin[x], LOW);

  digitalWrite(ledPin[currentLed], HIGH);
  currentLed += direction;

  if (currentLed == ARRAY_SIZE(ledPin)-1) direction = -1;
  if (currentLed == 0) direction = 1;
}

marco_c you are awesome sir. Thank you very much for taking some time to look at my problem and for your explanations. The ARRAY_SIZE macro was a great change to the code.