I ran into a weird problem yesterday while testing the functionality of the pulseIn() command. For this, I tried inputting a square wave with an offset(bias) so it varied between 0and 5V which is ideal for Arduino UNO. I've initialized analog pin 0 and fed this modified square wave to the same. Additionally I used the code available on the arduino website which goes like this :
Oddly enough, as soon as I ran the program, a string of "hhhh..." started coming in. To make things worse, too much bad data in the form of special characters such as a # or a % etc, started arriving at the serial monitor on the second attempt. These results are driving me crazy. Please suggest a way forward.
PS. The frequency of the square wave used is 100Hz.
That compiled? It is missing a ; after Serial.begin(9600)
When you define it like that, you end up treating pin 0 as a digital input.
Try using 14 and make sure you are connecting to A0 and not 0.
Altho I don't see any Serial.print(), so no idea why anything was being sent out.
Under File:Preferences, check the verbose output boxes, see if any errors are occurring.
That function prints an ASCII character defined by the given parameter (0-255). I don't think you want to do that. I think you want to use Serial.println(duration) which prints a human readable representation of its parameter.
Thanks for the reply,
I've corrected the line and used Serial.println instead. However, the readings on the serial monitor are not at all what's expected. The results are atrocious and this is a serious setback to a newbie like me
Do you think the baud rate I used is wrong? Will it impact when I use a square wave whose frequency is 100Hz on the analogPin 0 on the arduino UNO? Please enlighten me.
Thank you