I m starting arduino, and i got stuck with the code below.
The led pulse are ok, but i got only the first 1 with my println.
I m sure I made a mistake, anyone can help me to understand it plz?
Thanks a lot
const int LED = 13;
int sleep = 300;
int eta = 1;
int end = 10;
void setup() {
pinMode(LED,OUTPUT);
Serial.begin(9600);
}
void loop() {
while (eta < end)
{
Serial.println(eta);
code(eta);
delay(sleep/2);
eta++;
if (eta == end)
{
Serial.println("well done, you make a loop");
}
}
}
void code(int number) {
for (int i = 1; i <= number; i++) {
digitalWrite(LED,HIGH);
delay(sleep);
digitalWrite(LED,LOW);
delay(sleep);
}
}
aarg:
Does your LED have a current limiting resistor?
const int LED = 13;
...
void setup() {
pinMode(LED,OUTPUT);
As the OP says
Absolutly no idea, i havent add anything, i just use mother board and USB connector
I am going to be generous and assume until told different that he/she is using the built in LED on pin 13 and, hence, it has a current limiting resistor on the board
As the OP saysI am going to be generous and assume until told different that he/she is using the built in LED on pin 13 and, hence, it has a current limiting resistor on the board
Probably, but i m working on it since about 30 min, i m not sure what is "resistor", but i ll better tomorrow
telykin:
Elegon is maybe the problem.
Do you think about it ?
Sure, but isn't it Elegoo? I could be wrong. Anyway, this is really too vague a description of your hardware. Please tell us exactly what board you have. A link would be desirable.
... but to simply learn about a code loop, you shouldn't put it in loop() because that is a loop within a loop, as you found out after the fact. Just put it in setup().
pcbbc:
Your problem is that after eta becomes equal to 10, this while loop...
while (eta < end)
...is never true again. So nothing in the while “body” ever executes again.
Start loop() with...
eta = 1;
...if you want the loop to restart from 1 and count up to 9 again.
I have change my eta=1 in the loop, and i got the same result
const int LED = 13;
int sleep = 300;
int end = 10;
void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
Serial.begin(9600);// initialisation de la communication
}
void loop() {
int eta = 1;
while (eta < end)
{
Serial.println(eta);
code(eta);
delay(sleep/2);
eta++;
if (eta == end)
{
Serial.println("well done, you make a loop");
}
}
}
void code(int number) {
for (int i = 1; i <= number; i++) {
digitalWrite(LED,HIGH);
delay(sleep);
digitalWrite(LED,LOW);
delay(sleep);
}
}
aarg:
... but to simply learn about a code loop, you shouldn't put it in loop() because that is a loop within a loop, as you found out after the fact. Just put it in setup().
I made a try with all in the setup, no change
const int LED = 13;
int sleep = 300;
int end = 10;
void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
Serial.begin(9600);// initialisation de la communication
int eta = 1;
while (eta < end)
{
Serial.println(eta);
code(eta);
delay(sleep/2);
eta++;
if (eta == end)
{
Serial.println("well done, you make a loop");
}
}
}
void loop() {
}
void code(int number) {
for (int i = 1; i <= number; i++) {
digitalWrite(LED,HIGH);
delay(sleep);
digitalWrite(LED,LOW);
delay(sleep);
}
}
aarg:
Sure, but isn't it Elegoo? I could be wrong. Anyway, this is really too vague a description of your hardware. Please tell us exactly what board you have. A link would be desirable.
Your first code example will print the output “1..9” and then “well done, you make a loop” over and over ad infinitum.
Your second example will only do it once, since it is in setup, and setup only runs once.
Either:
a) you made a mistake and this is not the code you actually tried
b) we don’t properly understand what you mean by “ i got only the first 1 with my println.”
If you mean you only see “1” and no other output on the serial, then my guess would be you have a fault with your wiring. Try disconnecting the LED.
pcbbc:
You can’t have tested the first example properly.
Your first code example will print the output “1..9” and then “well done, you make a loop” over and over ad infinitum.
Your second example will only do it once, since it is in setup, and setup only runs once.
Either:
a) you made a mistake and this is not the code you actually tried
b) we don’t properly understand what you mean by “ i got only the first 1 with my println.”
If you mean you only see “1” and no other output on the serial, then my guess would be you have a fault with your wiring. Try disconnecting the LED.
I teach ESL so I am not criticizing your English, but it is almost unintelligible. As bad as it is, I think if you clearly express yourself in your native language, and then use Google translate, it would be easier for English speakers to understand your posts.
It's nearly impossible to make sense out of statements like, "the first 1 was already print from other try." 1 what? What other try? Try what? etc...
The videos are not helpful because, who has the time to sit through minutes and minutes of blah?
aarg:
I teach ESL so I am not criticizing your English, but it is almost unintelligible. As bad as it is, I think if you clearly express yourself in your native language, and then use Google translate, it would be easier for English speakers to understand your posts.
It's nearly impossible to make sense out of statements like, "the first 1 was already print from other try." 1 what? What other try? Try what? etc...
The videos are not helpful because, who has the time to sit through minutes and minutes of blah?
Sorry for my english, i will try google translation.
The video lasts just 40 seconds, it shows that the LED 13 flashes well and that tx also lights up correctly.
The only thing that doesn't work is the display in the console.
Finally, the first 1 is displayed, but then nothing.
I tried on different USB port, I get the same result.
I use an elegoo R3 card, a macbook pro and version 1.8.13 of arduino.
A person in the previous messages to test my code and it obviously works correctly on his side.
I no longer have too much idea to make the display work properly in the console.
/*
ASCII table
Prints out byte values in all possible formats:
- as raw binary values
- as ASCII-encoded decimal, hex, octal, and binary values
For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII
The circuit: No external hardware needed.
created 2006
by Nicholas Zambetti <http://www.zambetti.com>
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/ASCIITable
*/
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// prints title with ending line break
Serial.println("ASCII Table ~ Character Map");
}
// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example, '!' is the same as 33, so you could also use this:
// int thisByte = '!';
void loop() {
// prints value unaltered, i.e. the raw binary version of the byte.
// The Serial Monitor interprets all bytes as ASCII, so 33, the first number,
// will show up as '!'
Serial.write(thisByte);
Serial.print(", dec: ");
// prints value as string as an ASCII-encoded decimal (base 10).
// Decimal is the default format for Serial.print() and Serial.println(),
// so no modifier is needed:
Serial.print(thisByte);
// But you can declare the modifier for decimal if you want to.
// this also works if you uncomment it:
// Serial.print(thisByte, DEC);
Serial.print(", hex: ");
// prints value as string in hexadecimal (base 16):
Serial.print(thisByte, HEX);
Serial.print(", oct: ");
// prints value as string in octal (base 8);
Serial.print(thisByte, OCT);
Serial.print(", bin: ");
// prints value as string in binary (base 2) also prints ending line break:
Serial.println(thisByte, BIN);
// if printed last visible character '~' or 126, stop:
if (thisByte == 126) { // you could also use if (thisByte == '~') {
// This loop loops forever and does nothing
while (true) {
continue;
}
}
// go on to the next character
thisByte++;
}