Seeking help with Serial.parselnt()

Hi, I’m totally new to C++ (2 weeks). I retired a couple years ago and trying to keep my mind viable I took on C++. I’m using an ARDUINO UNO board to work with.
I have 2 questions.
First: All the materials I read use <iostream> as the heart of the application, if this be the case why does SKETCH give this message (Arduino: 1.8.0 (Windows 7), Board: "Arduino/Genuino Uno" Error compiling for board Arduino/Genuino Uno.) each time I try to use it?
Second: I’m writing my first code and have an issue I can’t find a fix for. It is dealing with Serial.parseInt(). The lines in question are 44-60. If I could get someone to look this over and give some advice I would be most grateful. Each input I enter always comes back zero (0). Thanks Walter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <dht.h>

dht DHT;

#define DHT11_PIN 7
#define LEDR 2
#define LEDB 3
#define LEDY 6

#define Skip_1

float x; 
float nm;
float HR;
float SEC;

 int SETTEMP;
 int SETDIF;
 int SETHUM;
 int DIF;
 int fartemp;
 String CMODE;
 int Check_Mode;
 int up_down;
  
void setup(){
  Serial.begin(9600);
  SETTEMP=72;   //  set test Defaults
  SETDIF=3;
  SETHUM=50;    //  end Defaults
  SEC=15;
  
  x=0;Check_Mode=0;up_down=0;DIF=0;fartemp=0;
  
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(6,OUTPUT);
}
void loop()
{ 
bool Skip=false;       // skip this code until fix found
while (Skip){
//               ********************lines with problem **** 
  if(x==0){
  SETTEMP=Serial.parseInt();                      
  Serial.print("What is the Temperature Setpoint? ");      
while (Serial.available()==0)  {
  }
  Serial.println(SETTEMP);

  SETDIF=Serial.parseInt();                      
  Serial.println("What is the Differential Setpoint? ");      
while (Serial.available()==0)  {
  }
  SETHUM=Serial.parseInt();                      
  Serial.println("What is the Humidity Setpoint? ");  
while (Serial.available()==0)  {
  }
  }
}                      // Returned to code here  
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature F = ");
  int celtemp=DHT.temperature;
  fartemp=(celtemp*1.8)+32;
  Serial.print(fartemp);
  Serial.print("  Setpoint: ");
  Serial.print(SETTEMP);
  Serial.print(" Differential: ");
  Serial.println(SETDIF);
  int phumidity=DHT.humidity;
  Serial.print("   Humidity % = ");
  Serial.print(phumidity);
  Serial.print("  Setpoint: ");
  Serial.println(SETHUM);
  delay(SEC*1000);
 
  //   testtemp=SETTEMP+SETDIF;
    x=x+1;
    nm=x*SEC/60;
   if (nm>59){
    HR=int(nm/60);
    nm=nm-int(HR*60); 
   }
   
DIF=abs(fartemp-SETTEMP);

if(DIF>SETDIF){
  Check_Mode=9;            // Heat or Cooling on flag set
  up_down=fartemp-SETTEMP;
  if(up_down<0){
    
  digitalWrite(LEDB,LOW); // Cooling
  digitalWrite(LEDR,HIGH); // Heat
    CMODE=("HEATING ON");
   }
  else
  {
    digitalWrite(LEDR,LOW);  // Heat
    digitalWrite(LEDB,HIGH); // Cooling
    CMODE=("COOLING ON"); 
  }
  }
                          // Test H/C flag 
if(Check_Mode!=9)
{
    digitalWrite(LEDR,LOW);
    digitalWrite(LEDB,LOW);
    CMODE=("            ");
}
Check_Mode=0;up_down=0; //    clear variables

  Serial.print("\n\n\n\n\n\n\n\n\n\n\n\n\n # Reads: ");
  Serial.print(x,0);
  Serial.print(" Total Run Time: ");
  Serial.print(HR,0);
if (HR>1){Serial.print(" Hours and ");} else 
  Serial.print(" Hour and ");
  Serial.print(nm,2);
  Serial.println(" Minutes"); Serial.println();
  Serial.print("      Current Readings          " );Serial.println(CMODE);
  Serial.println();
 }
First of all Arduino is not C++.
Secondly, you should only call Serial.parseInt() when Serial.available() returns non-zero. Otherwise, it will return you 0.
closed account (48T7M4Gy)
The Arduino 'platform' is written in C++ but not all of C++ is implemented because the Arduino only has certain (microcontroller) capabilities like driving the Serial interface.

Why do you want <iostream>?

http://playground.arduino.cc/Main/DHTLib
Last edited on
Thanks for your reply. The examples of code to try in most of the books I am reading use the <iostream> (Fundamentals of C++ Programming by Richard L. Halterman) very first example very first line of code is to include it. But as liuyang pointed out Arduino is not C++. This information now explains why some of the capabilities are not going to be available. Does this mean that user input to set SETTEMP, SETDIF and SETHUM is not possible and will have to keep these hard coded in the program like I'm doing for testing? This project is to be used in my greenhouse I wanted to use this board because it is very suited for this application. I'm open to any suggestions on boards and compilers that will use a better range of the C++ facilities. Other than the user input the code is working to turn on and off the relays for heating and cooling. Not to say there isn't some tweaking that I want to do.
Also thanks kemort for the link to playground.
It is possible to set your variables from user input.
But it's not from keyboard.

Basically you're having a correct direction.
Based on your current implementation, you could attach some hardware which supports transforming wireless, bluetooth or some other form data into serial data (an example hardware would be zigbee).
After that you could send your settings from your computer or other devices to your Arduino board to control it.
closed account (48T7M4Gy)
Cheers, good luck with the Arduino, they're a lot of fun.
liuyang wrote:
First of all Arduino is not C++.

The Arduino IDE makes slight mechanical changes to the user's source code and then invokes (an Atmel port of) G++ named avr-g++. For most purposes, the input language is C++.

From what I have discovered, the Arduino platform generates function prototypes, and the main function is defined elsewhere and does some initialization first, then calls setup() and then loop() repeatedly. The initialization stuff is pretty straightforward if you dig a little bit.

All the materials I read use <iostream> as the heart of the application

There is limited C++ standard library support for embedded processors in general. Since the Arduino's microcontroller is free-standing, there's no system available to provide the facilities the standard library needs to function.

Read from a serial port using USART or (if possible) over the USB connection.

Last edited on
Thanks everyone, this is a big help. I will take mbozzi's advice "dig a little bit" that is the way I learn best. Hope you all have a Great New Year. Walter
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
if( x == 0 )
{
    ???
    
    SETTEMP = Serial.parseInt();
    Serial.print("What is the Temperature Setpoint? ");
    
    while ( Serial.available() == 0 )
    {
        ???
    }
    
    Serial.println(SETTEMP);
    
    SETDIF=Serial.parseInt();
    Serial.println("What is the Differential Setpoint? ");
    
    while ( Serial.available() == 0 )
    {
        ???
    }
    
    SETHUM=Serial.parseInt();
    
    Serial.println("What is the Humidity Setpoint? ");
    while ( Serial.available() == 0 )
    {
        ???
    }
}
} 


@Wally2u

I decided to have a look at your code and a couple of points arise:

1. You code compiles without error using the standard Arduino IDE. This obviously is a plus and combined with my comments on your numerous while loops my guess is it's program logic and/or lack of a device communication that's the problem.

2. The problems you are having are not clear. It is good practice to state clearly what you are attempting to do, what the inputs are, what the expected outputs are, and what actually happened including full details of any error messages.

3. As a suggestion, and of course it's your choice, you might consider a clearer layout of your code by using whitespace and adding comments, pre/post condition statement even.

4. The code snippet I have attached is a direct copy of your code but clearly shows that there are possibly a number of infinite loops, who knows. Good and simple debugging practice, especially because of how Arduino's work is to include a dummy message inside the loop for display to the console. I've indicated with a ??? where I would have put them. ( Turning an LED on is another exotic option. )

5. Needless to say I have no idea about the development cycle you went through to end up with your 123 lines of program. Aside from having an overall plan of some sort, good practice dictates testing as you go. I assume you reached a point where the program crashed or stopped doing what you wanted (went silent due to an infinite loop) before 123 lines were written.
Topic archived. No new replies allowed.