Pages: [1]   Go Down
Author Topic: Serial Monitor Problems, Please Help  (Read 479 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,
Newbie here,
I was trying to make the serial monitor output to my windows computer using a USB connection.  I was initially just going to output the data being read in on an analog output.  I couldn't make that work. Then I put in a line just to output Hello World. Niether are coming on the serial monitor.  I looked at the language reference to no avail.

Please Help.  The code is here:

Quote
int counter = 0;
 int sensorPin = A0;
 int sensorValue = 0;
 
void setup()
{                
  pinMode(A0, INPUT);
  pinMode(10, OUTPUT);  
  pinMode(2, OUTPUT);
  Serial.begin(9600);      // open the serial port at 9600 bps:
  
}

void loop()
{
  // PWM Code
  while(counter < 50)
  {
   digitalWrite(10, HIGH);
   delay(5);
   digitalWrite(10, LOW);
   delay(5);
   }
   delay(50);
 
   // Thermostat Circuit
   //Serial.print(Hello World);
   Serial.print("Hello world.");
   sensorValue = analogRead(sensorPin);
   Serial.print(sensorValue,DEC);  // Output to the serial monitor
   if (sensorValue < 50) //500 is original Value for the temperature input
   {
    digitalWrite(2, HIGH); // turn fan on
   }
   else
     {
    digitalWrite(2, LOW); // turn fan off
     }
}
//}


« Last Edit: June 13, 2011, 11:37:35 am by power453c » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12894
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  while(counter < 50)
  {
   digitalWrite(10, HIGH);
   delay(5);
   digitalWrite(10, LOW);
   delay(5);
   }

What is the value of counter when the while loop is entered?  What is the value of counter for the second pass through the while loop?
Logged

Arizona USA
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Retired 35 years as programmer/engineer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Move the hello, world print to the setup function. Just after the Serial.begin.

Also, I put a delay(2000) immediately after the Serial begin to allow time for me to activate the Serial Monitor. Not sure if needed.

In any event, moving the hello, world OUTSIDE all the analog stuff will tell you if you have a printing problem or a 'something else' problem.
  --jim
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49252
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Also, I put a delay(2000) immediately after the Serial begin to allow time for me to activate the Serial Monitor. Not sure if needed.
The rest of us are. It isn't. Opening the Serial Monitor resets the Arduino, if the Arduino has not been modified to prevent that.
Logged

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

Thanks all, the printer does now print the first line.  I modified the code accordlingly:

int counter = 0;
 int sensorPin = A0;
 int sensorValue = 0;
 
void setup()
{               
  pinMode(A0, INPUT);
  pinMode(10, OUTPUT);   
  pinMode(2, OUTPUT);
  Serial.begin(9600);      // open the serial port at 9600 bps:
 
}

void loop()
{
  Serial.print("This is a test");
  // PWM Code
  while(counter < 50);
  {
   digitalWrite(10, HIGH);
   Serial.print("pin 10 is high");
   delay(5);
   digitalWrite(10, LOW);
   Serial.print("pin 10 is low");
   delay(5);
   }
   delay(50);
   int counter = 0;
   Serial.print("counter is now reset");
 
   // Thermostat Circuit
  digitalWrite(13, HIGH);
 
   Serial.print("Hello world it's temperature measurement Time.");
   sensorValue = analogRead(sensorPin);
   Serial.print(sensorValue,DEC);  // Output to the serial monitor
   if (sensorValue < 50) //500 is original Value for the temperature input
   {
    digitalWrite(2, HIGH); // turn fan on
    Serial.print("Fan is now on");
   }
   else
     {
    digitalWrite(2, LOW); // turn fan off
    Serial.print("Fan is now off");
     }
     delay(1000);
    digitalWrite(13, LOW);
}

Problem now is only the first line prints, then it seems to stall in the while loop.  Any thoughts on how to get it past this one.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49252
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Go back and read all the replies. There is nothing in the while loop that changes the value of counter, so the while statement is always true.
Logged

Pages: [1]   Go Up
Jump to: