hi, i'm a student just getting to know an arduino uno.
i'm trying to build a very basic code, but can't make it work.
i got two loops
reads the A5 and translate it to voltaje, and increase a variable
print to serial every 1 sec the count variable
this is what i got:
int contar = 0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop()
{
int sensorValue = analogRead(A5);
float voltage = sensorValue * (5.0 / 1023.0);
contar = contar + 1;
}
void imprimir ()
{
Serial.println(contar);
delay (1000);
}
thanks for the help.
regard
Yosi
You can't use delay() when you want two things to be happening 'at the same time'. Use millis() to schedule periodic events like "print once every second".
void loop()
{
static unsigned long previousMillis = 0;
int sensorValue = analogRead(A5);
float voltage = sensorValue * (5.0 / 1023.0);
contar = contar + 1;
if (millis() - previousMillis >= 1000)
{
Serial.println(contar);
previousMillis = millis();
}
}
Welcome to the forum
yosivikingo:
this is what i got:
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
When you posted your code without code tags did you receive a warning message ?
As to your problem, see Using millis() for timing. A beginners guide , Several things at the same time and the BlinkWithoutDelay example in the IDE
1 Like
Hi John, it should be one after another. meaning:
count how many A5 read where made and save it as "contar"
after that
print to serial contar, and contar should set to 0 again
and loop 1 should run again and loop2 after that.
thanks and regards
Yosi
Hi John, your code output got negative values, i'm trying to count how many read of A5 are made every 1000ms :
-22944
-14023
-5093
3829
12760
21681
30611
-25995
-17074
-8143
778
9709
18631
27561
-29045
-20124
-11193
So every second you want to know how many times the input was read in the previous second? Just change it to:
if (millis() - previousMillis >= 1000)
{
Serial.println(contar);
previousMillis = millis();
contar = 0; // add this line
}
yosivikingo:
int contar = 0;
An 'int' on an Arduino UNO can only count up to 32767. For higher values, change 'int' to 'unsigned long'.
Thank you john, now it working
this are the output that i got
8931
8922
8930
8922
8931
8930
8922
8931
8921
8931
8921
8931
8931
8921
8931
8922
8930
that should be how many A5 reads were made?
Yes, is there any doubt in your mind?
How many times loop() ran, which includes reading A5, incrementing 'contar', calling millis(), subtraction, comparison... If you add anything to loop(), like using the value of 'voltage' for anything' the rate will go down.
nope thanks a lot for asking
Yes, what is the significance of the number of reads? Don't you want to control the timing of them?
system
Closed
December 18, 2022, 4:21pm
13
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.