Serial.read and Serial.println problem

I am trying to get input a 1-4 digit number through the serial monitor and assign it to a variable. I have tried looking at the variations of Serial.read but got confused. I thought that I had it right in the code below, but it keeps returning a couple lines that look like the picture I attached. In the picture I put in 23. I want it to return "You want: 23". Any help is greatly appreciated.

long int num;

void setup() {
    Serial.begin(9600);
    delay(2000);
    Serial.println("How many do you want?");
}
 
void loop() {
    if(Serial.available()>0){
        num = Serial.read();
        Serial.print("You want: ");
        Serial.println(num,DEC);
    }
}

Serial.read() returns a single char. The Serial Monitor sends a string like "23" which is not the same as an integer with value 23. The ASCII char '2' is 50, '3' = 51...

Read this tutorial: Serial Input Basics - updated

Thank you! It is now working for that. My next step in this project is to get the inputted numbers to display on a screen. I just tried to edit the code and it didn't work at all when the screen was connected. I thought it might have been the code I added in, so I tried taking it out, but as long as the screen is connected, the serial monitor will not display what I put in. If I disconnect, it still works, but I need it to be able to talk to the display.

#include <genieArduino.h>
Genie genie;

const byte numChars=4;
char receivedChars[numChars];
boolean newData=false;

void setup() {
    Serial.begin(9600);
    genie.Begin(Serial);
    delay(500);
    Serial.println("How many do you want?");
}
 
void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker(){
  static byte ndx=0;
  char endMarker = '\n';
  char rc;

  while(Serial.available()>0 &&newData==false){
    rc=Serial.read();

    if(rc != endMarker){
      receivedChars[ndx]=rc;
      ndx++;
      if(ndx>=numChars){
        ndx=numChars-1;
      }
    }
    else{
      receivedChars[ndx]='\0';
      ndx=0;
      newData=true;
    }
  }
}

void showNewData(){
  if(newData==true){
    Serial.print("New price is $0.");
    Serial.println(receivedChars);
    **genie.WriteObject(GENIE_OBJ_LED_DIGITS,0,receivedChars); //tried it without this line**
    newData = false;
  }
}

I have no idea what a "genie" display is or how it operates. If connecting it causes your program to stop/crash, maybe it is wired wrong. Have you tried any of the examples that come with that library?

Yes. I have used the Arduino with it already to display numbers and calculations. It works perfectly fine with static numbers, but the issue lies where I need to change the current price. I am using it to display a current price and then how much of the product has been dispensed as well as the total price. The price changes over time, even as it is actively being dispensed, hence needing to change the variable while running the program. I will try to go to the forum page for this display to see if anyone there has a solution. Thanks again for your help.

You have attached the genie to the single serial port, hence it isn't talking to the monitor anymore.

Try using software serial for the display.

I looked up SoftwareSerial and attempted to make it work, but it still doesn't seem to want to. Could you take a look and see if you can figure out where I went wrong?

#include <genieArduino.h>
#include <SoftwareSerial.h>
Genie genie;

SoftwareSerial mySerial(2,3);

const byte numChars=5;
char receivedChars[numChars];
boolean newData=false;

void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);
    genie.Begin(mySerial);
    delay(500);
    Serial.println("How much is it?");
}
 
void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker(){
  static byte ndx=0;
  char endMarker = '\n';
  char rc;

  while(Serial.available()>0 &&newData==false){
    rc=Serial.read();

    if(rc != endMarker){
      receivedChars[ndx]=rc;
      ndx++;
      if(ndx>=numChars){
        ndx=numChars-1;
      }
    }
    else{
      receivedChars[ndx]='\0';
      ndx=0;
      newData=true;
    }
  }
}

void showNewData(){
  if(newData==true){
    Serial.print("New price is $0.");
    Serial.println(receivedChars);
    genie.WriteObject(GENIE_OBJ_LED_DIGITS,0,receivedChars);
    newData = false;
    Serial.println("How much is it?");
  }
}

genie.WriteObject(GENIE_OBJ_LED_DIGITS,0,receivedChars);

I have no knowledge of the Genie, but In the library I see

uint16_t    WriteObject    (uint16_t object, uint16_t index, uint16_t data);

You are trying to pass it a character array with received chars.

Try use atoi on the receivedChars and pass the integer to your .writeObject.

I see several WriteStr() functions, but none appear to include the Object.

Can you show us the code which successfully prints the static number to the display. Are you using the integer 123 or the character string "123" when you do the write?

#include <genieArduino.h>
Genie genie;

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

genie.WriteContrast(7);
}

void loop() {

long int price = 15;
long int kW = 0;
long int total = 0;
genie.WriteObject(GENIE_OBJ_LED_DIGITS,0,price);

if(price>0){
for(kW=0;kW<6001;kW++){
genie.WriteObject(GENIE_OBJ_LED_DIGITS,1,kW);
total = kW*price*0.01L;
genie.WriteObject(GENIE_OBJ_LED_DIGITS,2,total);
delay(500);
}
}
}

It is using ints in the previous code I had. I'm not sure how I can read a multi digit number from the Serial Monitor and save it in an integer variable.

@cattledog already mentioned atoi() which stands for Ascii to Integer. E.g. it converts a string into a number. Google will overwhelm you with descriptions/examples/etc.

Sorry, I must have missed that. That part is working now. Thank you all so much. Now I just need to figure out the genie.WriteObject to get the int into the object on the screen. I have it as an int and it is able to print into the Serial Monitor, but even using SoftwareSerial it isn't communicating with the display.

void showNewData(){
  if(newData==true){
    Serial.print("New price is $0.");
    price = atoi(receivedChars);
    Serial.println(price);
    genie.WriteObject(GENIE_OBJ_LED_DIGITS,0,price);
    newData = false;
    Serial.println("How much is it?");
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.