Pages: [1]   Go Down
Author Topic: analogRead() in loop() stops Serial.print() from working in setup()  (Read 1242 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup()
{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.print(analogRead(5)); // reads from microphone
}

I receive the message "test" only if I comment out the line in loop(). Putting a delay in loop() doesn't help. I'm not sure what's wrong here, does anyone want to explain?
Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried this on my Arduino Uno with 0023 and the serial monitor displays the reading from the analog input. Strange that it doesn't work for you.
Logged


Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. I did your code. The analog did read something, and it was going "nuts"...
So I change a bit, put a delay in loop() function, and I have a reading and the test did show. I simply place a piece of wire at analog pin 5. The reading was a floating value ( any values from 0 to 1024 ) which I was expecting.

I don't know what you did ( the hardware section ), but if you need to read a mic, you need some amplification and make sure the signal goes from 0 V to 5 V. Not in the minus range... it will damage your Atmega chip.  

Here the test code.

Code:

void setup()
{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.println(analogRead(5));
  delay(1000);
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a Duemilanove, and I need to read the microphone faster than once per second (the faster the better). It just seems like the code never runs properly when I try this. I am sure the microphone is giving values from 0V to 5V, already tested.
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18798
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Microphone? Can you show your hardware setup?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's this; http://www.sparkfun.com/products/9964

And I'm sure the circuitry with the microphone works just fine. The code is the problem.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That sounds pretty strange.

I wonder whether the problem is caused by Serial.print() or analogRead(5).

Suggest you try printing an arbitrary int, and calling analogRead() (but not printing the result) and see whether either of them on their own is causing the problem.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18798
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Which version of the IDE? Try this instead:

Code:
void setup()
{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.println(analogRead(5), DEC); // reads from microphone
}

Note the println rather than print, and making sure we see decimal.
Logged


Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@xvedejas

That should work fine, as long you connect properly, like +5 to +5 of the Ardiuino, GND with the Ardiuno, and Out at analog pin 5. You should expect a reading value around half of 1024 , if the level of sound in your room is quiet. I use my code modification ( with the delay(1000); or Nick Gammon code, but add a delay(1000); because the serial display will go fast, ( fast scrolling ) Heh you can still use Nick' code and disconnect the Arduino so you see the result of the serial output.

Quote
I have a Duemilanove, and I need to read the microphone faster than once per second (the faster the better). It just seems like the code never runs properly when I try this. I am sure the microphone is giving values from 0V to 5V, already tested.
 

I am sure the code is fine. The way the code is, that the fastest you can get. It only got one line in loop()... it Serial.println(analogRead(5));  That it ! And you should have a reading about 300 to 700 <-- +/- 2.5 V, but on average close to 500.

Now, that make sense.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26312
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The reading was a floating value ( any values from 0 to 1024 )
I'd be worried if analogRead returned a value of 1024.  smiley-wink
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18798
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup()
{
  Serial.begin(9600);
  Serial.println("test");
}

void loop()
{
  Serial.print(analogRead(5)); // reads from microphone
}

I receive the message "test" only if I comment out the line in loop(). Putting a delay in loop() doesn't help. I'm not sure what's wrong here, does anyone want to explain?

...

It's this; http://www.sparkfun.com/products/9964

And I'm sure the circuitry with the microphone works just fine. The code is the problem.

Excepting a hardware issue, your code will print "test".
Logged


Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18798
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Step 1. Disconnect all external devices (eg. microphones) and cables, except the cable connecting the Arduino to your PC, and run the test again. You should see random values shoot by as the analogRead picks up noise.
Logged


Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, AWOL, I mean 1023, happy  smiley-lol
Logged

Pages: [1]   Go Up
Jump to: