Pages: [1]   Go Down
Author Topic: Serial Input confused on type SOLVED  (Read 603 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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(){
}

« Last Edit: February 20, 2013, 10:56:30 am by grendle » Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 http://arduino.cc/en/Serial/Read?action=sourceblock&num=1

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);
        }
}
 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Nick Gammon
Logged


Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Code:
for (int a=0; a<inByte, DEC; a++){
like it works for serial print. thanks tho.

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50307
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for (int a=0; a<inByte, DEC; a++){
What do you think "inByte, DEC" is doing? Not what you think, I'm pretty sure.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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);
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
inByte = inByte - '0';

before your for loop.

Mark
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50307
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3441
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
 
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4336
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this as a starting point
Code:
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
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Pages: [1]   Go Up
Jump to: