Go Down

### Topic: Short, quick problem with Integers (Read 90 times)previous topic - next topic

#### MoistSpaghetti

##### Oct 12, 2017, 08:47 pm
I'm writing a bit of code for my Intro to Engineering class, and I can't seem to get the "blink" function to work. Looking at the code, I believe it's a problem with the "Number" integer not changing.
When the code is ran: The LEDs, 1-8, lights up sequentially, like a status bar. But, it just keeps repeating this process, even though it should blink instead.
Code: [Select]
`/*The idea of this is to make a row of LEDs light up like a status bar, one at a time sequentially, and once all of them are lit, wait a second then turn them off. After a number of cycles, flash the LEDs then do nothing. */ // declaring all global variables void stat() { // lights LEDs 1-8 up one at a time, then turns them all off at the same time digitalWrite(8, HIGH);      delay(1000);                 digitalWrite(1, HIGH);      delay(1000);                 digitalWrite(2, HIGH);      delay(1000);                 digitalWrite(3, HIGH);      delay(1000);                 digitalWrite(4, HIGH);      delay(1000);   digitalWrite(5, HIGH);    delay(1000);                 digitalWrite(6, HIGH);      delay(1000);                 digitalWrite(7, HIGH);      delay(1000);    digitalWrite(8, LOW);   digitalWrite(1, LOW);   digitalWrite(2, LOW);   digitalWrite(3, LOW);   digitalWrite(4, LOW);   digitalWrite(5, LOW);   digitalWrite(6, LOW);   digitalWrite(7, LOW);      delay(1000);      }void blink() { // blinks LEDs 1-8 on for 250ms, then off.  digitalWrite(8, HIGH);                   digitalWrite(1, HIGH);                  digitalWrite(2, HIGH);                    digitalWrite(3, HIGH);                digitalWrite(4, HIGH);      digitalWrite(5, HIGH);               digitalWrite(6, HIGH);                  digitalWrite(7, HIGH);    delay(250);   digitalWrite(8, LOW);                   digitalWrite(1, LOW);                  digitalWrite(2, LOW);                    digitalWrite(3, LOW);                digitalWrite(4, LOW);      digitalWrite(5, LOW);               digitalWrite(6, LOW);                  digitalWrite(7, LOW); }  int Number; // declaring the Number int, used to deterine how many times the LEDs have flashed.void setup() {  // initializing pins as outputs  pinMode(8, OUTPUT); // LED 1  pinMode(1, OUTPUT); // LED 2  pinMode(2, OUTPUT); // LED 3  pinMode(3, OUTPUT); // LED 4  pinMode(4, OUTPUT); // LED 5  pinMode(5, OUTPUT); // LED 6  pinMode(6, OUTPUT); // LED 7  pinMode(7, OUTPUT); // LED 8   }// loops to check if number > 1, if so it runs "blink", else runs "stat" and adds 1 to int Numbervoid loop() {    if (Number > 1) // checks if Number > 1  {blink();  // runs "blink"}else {   stat(); // runs "stat"  int Number = Number + 1; // adds one to int number, I think?}}`

#### aarg

#1
##### Oct 12, 2017, 08:54 pmLast Edit: Oct 12, 2017, 09:00 pm by aarg
Code: [Select]
`  int Number = Number + 1; // adds one to int number, I think?`

Whenever you have doubts, consult documentation. Forums are a place for secondary problems - things that the documentation doesn't cover. For an intro course, you should not be afraid to ask a professor or teaching assistant such a question.

I think that your problem is that you declare Number inside the loop Function - thus it is initialized every time loop runs and is not using the previous value.

In other words, you said, "int Number" twice. That creates two variables called Number. Google "C++ scope".
... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

#### PaulS

#2
##### Oct 12, 2017, 08:55 pm
Quote
I believe it's a problem with the "Number" integer not changing.
Which Number integer? This one?
Code: [Select]
`int Number;`
Or this one?
Code: [Select]
` int Number = Number + 1;`
Why do you have two variables with the same name?

#### MoistSpaghetti

#3
##### Oct 12, 2017, 09:15 pm
Just realized I put "int" in there. That's my fault. Is it possible to delete this thread?

#### aarg

#4
##### Oct 12, 2017, 09:38 pmLast Edit: Oct 12, 2017, 09:39 pm by aarg
Just realized I put "int" in there. That's my fault. Is it possible to delete this thread?
Why? Someone else might learn from it. Unless "MoistSpaghetti" is your birth name, it won't haunt you.
... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Go Up