0

I've been learning how to program this fine piece of equipment but i seem stuck. I came upon the "Knight rider" example where you are supposed to blink LED's from one side to another and i can't understand where some of the code comes from. For example:

byte ledPin[] = {4,5,6,7,8,9,10,11,12,13};
int ledDelay;
int direction = 1;
int currentLED  = 0;
unsigned long changeTime;
int pot = 2;
int i;

void setup()
{
    Serial.begin(9600);

    for (i = 0; i < 10; i++)
    {
        pinMode(ledPin[i], OUTPUT);
    }

    changeTime = millis();
}

void loop()
{
    ledDelay = analogRead(pot);

    if ((millis()- changeTime) > ledDelay)
    {
        changeLED();
        changeTime = millis();
    }
}


void changeLED()
{
    for (i = 0; i < 10; i++)
    {
        digitalWrite(ledPin[i], LOW);
        Serial.print("i = ");
        Serial.println(i);
    }

    digitalWrite(ledPin[currentLED], HIGH);

    currentLED += direction;

    if (currentLED == 9)
    {
        direction = -1;
    }

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

In this code I don't know where the "direction" variable is coming from I searched on the arduino reference page but can't find it. Are there other references where I could look or am I misunderstanding something?

1

It's coming from the program itself:

int direction = 1;

That means "Create a signed integer called 'direction' and assign it the value 1.

| improve this answer | |
1

You defined the direction variable on the top with:

int direction = 1;
| improve this answer | |
  • Yes I am fully aware of that. But in the sketch it is coloured red, if i rename it to "dir_etc" for example it is not highlighted by the IDE. So i'm sure there is more to it than just me defining it. Hope my explanation is clear enoght. – calatorul.fraudulos Feb 13 '17 at 12:41
  • Hm, that's interesting. What IDE are you using? It could be defined as a global variable in one of the files that you are including, but that sounds very unlikely to me. What files are you including in your project? Also (depending on your IDE) you might be able to right click on the variable and chose "View variable declaration" or something like that. – kok_nikol Feb 13 '17 at 18:33
  • I'm using the official IDE v1.8.1 from the arduino site. Other than that i don't know to be using any files for this sketch, but then again I'm just a beginner so i might be wrong. Also i can't find the "View variable declaration" you're writing about. – calatorul.fraudulos Feb 13 '17 at 19:48
  • 1
    Ok, so we ruled out a couple of things. I'm using the same IDE, but I haven't had a similar thing happen to me. Can you post a screenshot of how the code looks like (how the direction variable is colored)? And post the error message you receive? – kok_nikol Feb 13 '17 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.