Offline
Full Member
Karma: 1
Posts: 128
|
 |
« on: February 20, 2013, 12:42:58 am » |
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(){ }
|
|
|
|
« Last Edit: February 20, 2013, 10:56:30 am by grendle »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 12
|
 |
« Reply #1 on: February 20, 2013, 12:54:15 am » |
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=1int 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
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14118
Lua rocks!
|
 |
« Reply #2 on: February 20, 2013, 01:22:32 am » |
... and started on Nick Gannons tut few minutes ago...
Nick Ga mmon
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 1
Posts: 128
|
 |
« Reply #3 on: February 20, 2013, 09:56:47 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #4 on: February 20, 2013, 10:02:15 am » |
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
Full Member
Karma: 1
Posts: 128
|
 |
« Reply #5 on: February 20, 2013, 10:28:57 am » |
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
Full Member
Karma: 1
Posts: 128
|
 |
« Reply #6 on: February 20, 2013, 10:37:08 am » |
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
God Member
Karma: 12
Posts: 781
|
 |
« Reply #7 on: February 20, 2013, 10:40:31 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36476
Seattle, WA USA
|
 |
« Reply #8 on: February 20, 2013, 10:46:37 am » |
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
Edison Member
Karma: 51
Posts: 2192
|
 |
« Reply #9 on: February 20, 2013, 10:53:20 am » |
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
Full Member
Karma: 1
Posts: 128
|
 |
« Reply #10 on: February 20, 2013, 10:56:15 am » |
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
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #11 on: February 20, 2013, 11:00:53 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 1
Posts: 128
|
 |
« Reply #12 on: February 20, 2013, 11:11:59 am » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
|