Serial Input confused on type SOLVED

this snippet works fine, ultimately I am trying to have it set up where I can enter 1 to 540, to represent minutes. This is my testing code. I type in "1" and get 49 blinks on the led instead of "1" blink. I understand why after looking at the asc chart, but, my problem is how can I make 'inByte' = "1". Have gone through the tut's on playground and started on Nick Gannons tut few minutes ago but brain is fried for tonight. So far I'm confused on how to approach this. Any wisdom would be greatly appreciated. Thanks.

char inByte; // Byte input from command prompt

void setup(){
  Serial.begin(9600);
  Serial.println("Enter number");
  while (!Serial.available()) {
  }
  inByte = Serial.read();

  Serial.println(inByte);

  for (int a=0; a<inByte; a++){
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);

  }

  Serial.println("Done");


}
void loop(){ 
}

i am a bit of newb. but i don't understand why you are using a char instead of an int. do you think that this is the ascii number your printing ? http://www.asciitable.com/

// from arduino forums Serial.read() - Arduino Reference

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}

grendle:
... and started on Nick Gannons tut few minutes ago...

Nick Gammon

i am a bit of newb. but i don't understand why you are using a char instead of an int

i tried that, same results. the following does not work

for (int a=0; a<inByte, DEC; a++){

like it works for serial print. thanks tho.

Nick Gammon

my apologies, no offense intended, i was really really tired last night when i posted, and this was an honest mistake.

for (int a=0; a<inByte, DEC; a++){

What do you think "inByte, DEC" is doing? Not what you think, I'm pretty sure.

What do you think "inByte, DEC" is doing? Not what you think, I'm pretty sure.

i was just showing post 2 that - that way doesnt work like

Serial.print(inByte,DEC);

perhaps i didnt express myself correctly, im interested in my variable holding the decimal version of my serial input. im still working on the issue, as im not a programmer just a hobbyist, but i figured someone might point me in the right direction. some of the tutorials are written for people who already hold a decent knowledge of the language. i have built a weather station, that records temperature, humidity, and air pressure every minute and writes to sd card, which later translates nicely into a spreadsheet. now, instead of coding every time i want it to sample for a different time period, i thought i could change the time variable through serial input, since the station doesnt start sampling until a button press. some instances im needing 4 hours, others 6 or 8 hours. i thought it would be pretty cool to be able to set my time through the serial window, rather than having to change code, as ive said, every time. any ideas?

You typed in the char '1' so you must then read the char '1' not the number 1. Convert the char '1' to the number 1. Add

inByte = inByte - '0';

before your for loop.

Mark

i was just showing post 2 that - that way doesnt work like

Serial.print(inByte,DEC);

Of course it doesn't. The print() method takes two arguments, and does something with them. That behavior is related to the function, not the arguments.

grendle:
this snippet works fine, ultimately I am trying to have it set up where I can enter 1 to 540, to represent minutes.

Getting 1 to 9 is pretty trivial. Getting 10+ (2+ characters) is a different animal. To receive a string of characters and convert it to a number, you'll need a couple things:

An array to store the character plus a null terminating byte.
An index variable to keep track of where in the array you need to put the next character.
A termination byte that tells the Arduino when you're done sending data. If you're using the Serial Monitor, you can append new line or carriage return and use that.

You have to keep in mind that the loop() function runs very quickly and could run 100s of times between characters, so you can't just assume that the moment you hit enter all characters from the monitor will be available.

You typed in the char '1' so you must then read the char '1' not the number 1. Convert the char '1' to the number 1. Add

Mark, thank you very much. This is what i needed. i owe you a beer, so go grab one at the pub and tell em to put it on my tab!

I am at Mr. Gammons section on timing (section that i fully understand), and it is getting interesting now, and i cant wait until i have a complete grasp on serial comms, but you have solved a problem so i can finish a project, and start something else, again i thank you.

Try this as a starting point

int inInt;

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

void loop() 
{ 
  while (Serial.available() > 0)
  {
    inInt = Serial.parseInt();
    Serial.println(inInt);
  }
}

NOTE - depending on how you have the serial monitor set up the input will also include a carriage return, line feed, both or neither so you may get an extra digit output

thank you Bob, im gonna fiddle with that also, but i must apologize because i already brought Mark a beer, im all 'tapped out' no pun intended :slight_smile: