0

I am having troubles with my arduino micro. In my programming, I am using the serial. When I use println, nothing shows up in the serial monitor except for 0. So if I type 1, I get back 0 instead of 1, same goes for 3241234 or any random number. Here is the code:

void setup() {
  Serial.begin(9600); //Connect to the serial monitor console
}

void loop() {
    while (Serial.available() == 0); //Wait until Serial is available

    //Read val
    int val = Serial.read() - '0'; //Val that represents input
    delay(500);
    Serial.println(val);
}

So I have another problem and people have asked for the code. Basically, printing a string returns 0.

 void setup() {
  Serial.begin(9600); //Connect to the serial monitor console
}

void loop() {
    while (Serial.available() == 0); //Wait until Serial is available

    //Read val
    int val = Serial.read(); //Val that represents input

    delay(1000);
    if (val == 1) {
      Serial.print('Firing the motor');
    } else {
      Serial.print('Please press 1 to fire the motor.');
    }

    delay(4000);
}

Instead of printing Please press 1 to fire the motor, I get 0 in the console.

Thanks for the help!

2
  • How are you sending values to arduino device?
    – LPs
    Jun 17, 2016 at 10:12
  • @LPs I am using the serial monitor.
    – Tino Caer
    Jun 17, 2016 at 11:30

3 Answers 3

1

Try without

- '0'

You read your data and save into in int. then print that int, when you pass only the int, you get the int as an ASCII-encoded decimal. Why r u doing

- '0'

?

3
  • Hey, I am trying now to use my variable to print something So if I type 1, I want the console to say "Firing function", the problem is that the console prints 0. What is the problem here?
    – Tino Caer
    Jun 17, 2016 at 11:34
  • You are going to need to show the code. Its probably something simple to fix though. Jun 17, 2016 at 12:01
  • @Matt I have added the code in the question section, thanks.
    – Tino Caer
    Jun 17, 2016 at 12:06
0

This time you are comparing an integer 1 with a character '1'.

int val = Serial.read(); 

Is reading you pressing the one key and storing it in an integer variable as the number 49 (ASCII '1')

if (val == 1) {

You are now seeing it 49 is the same as 1, which it isn't.

To make your code easier stick to characters.
Change the Serial.Read line to:

const char val = Serial.read(); // Val that represents input 
                                // (const because it will not change).

and the if line to:

if (val == '1')  // '1' character 1 

By making val const you stop yourself accidentally changing it so if you do

if (val = '4') // Compilation error.

Hope that helps.

4
  • Hi Matt, thanks for the help. This is useful. I still cant print the strings I want to: Firing the motor or Please press 1 to fire the motor. I tried replace the ' ' with " " but that doesnt fix it either.
    – Tino Caer
    Jun 17, 2016 at 12:25
  • You are right you need double not single quotes so Serial.println("Firing the motor"); is what you need. println will print a whole line of text. I am guessing, but it might be something to do with the Serial.Read commands. Replace you print statements with Serial.write calls, you will need to look in the manual on www,arduino.cc to get the right parameters. Make sure you add \n\0 to the end of your strings when you use serial.write. Jun 17, 2016 at 12:31
  • Hey Matt, using .write returns the amount of characters in a string, so this is not helpful to me. I am going to keep looking, thanks for the help!
    – Tino Caer
    Jun 17, 2016 at 13:09
  • @TinoCaer Serial.write(“hello”); has the same superficial behaviour as Serial.print("hello"); You can ignore the return value. Having read the man page I can't see it will help though. :( Jun 17, 2016 at 14:27
0

To your second question, strings in C are surrounded by the double-quote character: ", not the the single-quote or apostrophe character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.