Trying to get a led to blink slower and slower

I got a kit offline from freenove trying to learn. Im so new it hurts I just want to get my led to blink slower and slower by one second. Its not working what am I missing? I assume the void is wrong what do i need to use instead of void?

1
2
3
4
5
6
7
8
9
10
11
12
13
void setup() {

}
void loop() {
  
  int i ;
  i = i + 1000 ;
  
 digitalWrite (4, HIGH);
 delay (i);
 digitalWrite (4, LOW);
 delay (i);
}
Let's back up. Have you, thus far, been successful in getting the LED to light up or blink, in any manner?

I am going to assume the answer is that is 'yes'. In that case, I think the problem is your understanding of the local variable 'i'. The variable is uninitialized, and its value resets every time you call your loop, because it's a local variable.

I am not familiar with the hardware/software platform you're working with, but you either need to be able to query the global time of the software, or you need some state that is outside of the loop function itself. The simplest way to do this is with a global variable, since I am assuming you cannot change how the loop function is called.

Also, use meaningful variable names. 'i' isn't really meaningful and should only be used in simple for loops as the de facto standard iteration variable.

1
2
3
4
5
6
7
8
9
10
int delay_ms = 0; // global variable!

void loop() {
  delay_ms += 1000;
  
  digitalWrite (4, HIGH);
  delay (delay_ms);
  digitalWrite (4, LOW);
  delay (delay_ms);
}


(Global variables are not usually recommended; in general, you should pass variables through functions/classes. But if you can't change the signature of this loop function, then that might not be feasible.)

PS: On terminology -- you don't call something a "void". You'd call it a "function" in general, or a "void function" if it's important to qualify that it doesn't return a value.
Last edited on
I can get the led to blink with this

1
2
3
4
5
6
7
8
9
10
11
12
13
void setup() {

}
void loop() {
  
  int i ;
  i = 1000 ;
  
 digitalWrite (4, HIGH);
 delay (i);
 digitalWrite (4, LOW);
 delay (i);
}


It worked awesome thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void setup () {
  
}

int delay_ms = 0; // global variable!

void loop() {
  
  delay_ms += 1000;
  
  digitalWrite (4, HIGH);
  delay (delay_ms);
  digitalWrite (4, LOW);
  delay (delay_ms);
}


I actually understand a little. Never been more happy to watch a led blink slower. What function can I use to return a value? Thanks in advance the tutorial I'm using is a bit hard to understand and jumps over things.


To learn more about functions, I would try these links.
http://cplusplus.com/doc/tutorial/functions/
https://www.learncpp.com/cpp-tutorial/function-return-values/

_____________________

A function that returns a value would be something like:

1
2
3
4
int quadruple(int n)
{
    return 4 * n;
}

(This, as the name implies, quadruples the value of the input, y = 4 * x.)

And you'd call it like:
1
2
int times4 = quadruple(42);
cout << times4 << '\n'; // or however you can print something on your hardware 


(PS: To not appear hypocritical: the reason that I used 'n' as a variable name here is because there's no other context to determine what meaning that variable has. If you as the programmer have more context to offer, then add that to the variable name if it makes sense. It's not hard-set rules.)
Last edited on
Thanks for pointing me in the direction I needed ill start reading.
Topic archived. No new replies allowed.