changing values over serial monitor

Hello. I'm new to Arduino and programming in general. I have been pulling my hair out for the last few days trying to figure out how to change a value over the serial monitor and would really appreciate some help. I am trying to change the number I have highlighted in the code below by typing the required value in to the serial monitor.

Here is the code

int reading =0;
int sensorPin = A0;
int relay =7;

void setup() {
pinMode(relay, OUTPUT);
Serial.begin(9600);
}

Void loop() {
reading = analogRead(sensorPin);
int celsius = reading/2;

if (celsius < 35) {
diditalWrite(relay, HIGH);
} else{
digitalWrite(relay,LOW);
}
delay(500);
}

thanks for looking and again I really appreciate any help you can offer. Thanks :slight_smile:

You can't have executable code outside of a function, in this case you're missing "loop()".

Also "relay" is not the same as "realy", and you're going to forget that stray "7" sooner or later, so fix that now too.

Also, "didital" != "digital"

(uncompiled, untested)

const byte  sensorPin = A0;
const byte  relay =7;

void setup() 
{
  pinMode(relay, OUTPUT);
  Serial.begin(9600);
}

void loop ()
{
  int reading = analogRead(sensorPin);
  int celsius = reading/2;

  if (celsius < 35) {
    digitalWrite(relay, HIGH);
  } else{
    digitalWrite(relay,LOW);
  }
  delay(500);
}

Thanks for the advice. I do have the code running on my other computer and have done a bad job of copying it over! :blush:

This demo and this one illustrate ways of reading data into an Arduino. Both demos work with the Serial Monitor although the second one is intended for use with a PC program.

...R

Very basic, uncompiled & untested

  static int threshold = 35;
  if (Serial.available ())
  {
    static int newThreshold;
    int inChar = Serial.read ();
    if (isdigit (inChar))
    {
      newThreshold *= 10;
      newThreshold += inChar - '0';
    }
    if (inChar == '\n')
    {
      threshold = newThreshold;
      newThreshold = 0;
   }   
  }
  if (celsius < threshold) {

Thanks guys! I will have a play around and see if I can get it working.

The below code is for servos, but might be of value for your project. A numeric value is sent to the arduino via the serial monitor. The string of bytes sent is captured as readString. The captured readString is then converted into a number. Then the number is available for use.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    myservo.write(n);
    readString="";
  } 
}

solved! thanks guys I finaly figured this one out with the code bellow. now off to write the python code! :~

String readString;
int reading = 0;
int sensorPin = A0;
int relay = 7;

void setup() {
Serial.begin(9600);
pinMode(relay,OUTPUT);
}

void loop() {

reading = analogRead(sensorPin);
int celsius = reading/2;

while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}

if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number

if (celsius < n){
digitalWrite(relay,HIGH);
}else{
digitalWrite(relay,LOW);
}
delay(500);

readString="";
}
}

You may want to rethink the logic there.
You will certainly want to start using code tags.

Wright:
solved! thanks guys I finaly figured this one out with the code bellow. now off to write the python code! :~

you can certainly make your new code more compact....

String readString;
int sensorPin = A0;
int relay = 7;

void setup() 
{
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
}
//
void loop() 
{
  int celsius = analogRead(sensorPin) / 2;
  while (Serial.available()) 
  {
    char c = Serial.read();
    readString += c;
    delay(2); 
  }
  if (readString.length() >0) 
  {
    Serial.println(readString); 
    int n = readString.toInt();
    digitalWrite(relay, celsius < n ? HIGH : LOW);
    delay(500);
    readString = "" ;
  } 
}

and, what does the delay accomplish? you should control the frequency of updates on the other side... if they are less than a half second, your serial buffer will fill up and you will have a problem because it will combine the two transmissions.

anyways, you should maybe practice detecting delimiters so that you avoid that problem completely...

transmit your data like this:

#114

so that you can stop buffering when you get to that next '#' character

just a suggestion: add some error checking. if you are controlling an environment for a pet, for example. you don't want to accidentally fry your fish.

Similar code using a comma , as a data delimiter, so no delay needed.

//zoomkat 3-5-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 700, or 1500, or 2000,
  //or like 30, or 90, or 180,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          myservo.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          myservo.write(n);
        }

        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}