I would like to simply input a number like 123456 into the serial port monitor and have it spit out that value in integer format.
What am I doing wrong my code never seems to reach the case '\n'. I put that in there because I thought that when it hit the end of the input it would output '\n'.
//Created by Jacob
int Led = 13;
int pos=0;
char n=0;
unsigned long val =0;
void setup() {
pinMode(Led, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0){
static char input_line [20];
n=Serial.read();
switch(n){
case '\n':
pos=0;
val=atoi(input_line);
Serial.print(val);
//I want val to be a integer so I can use it as an input.
//I would place a mathmatical function then a desired output here
default:
if (pos<19){
pos=pos+1;
input_line [pos]=n;
}
}
}
}
jacob84401:
I would like to simply input a number like 123456 into the serial port monitor and have it spit out that value in integer format.
What am I doing wrong
I believe the only thing wrong is that you are seeing a problem that isn't really there. You don't need to make a string.
Try this, and enter 123456:
void setup()
{
Serial.begin(9600);
Serial.println("type something in the space above and hit Send,");
}
void loop()
{
while(Serial.available()==0)
{}
delay(500);
while(Serial.available()>0)
{
Serial.write(Serial.read());// note it is Serial.WRITE
}
Serial.println("");
}
Simple code that converts a numeric string into a number for use with a servo.
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
Serial.println("servo-test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number
myservo.write(n);
readString="";
}
}
Nick_Pyner:
I believe the only thing wrong is that you are seeing a problem that isn't really there. You don't need to make a string.
Try this, and enter 123456:
Thanks for the input. I sure hope that it is just as easy as you say. Maybe I should give more information on my end goal. I would like to turn on and off electronics through sms messages. So it would look something like this "on12.50" meaning turn on for 12.50 minutes. I would also like to monitor other inputs while this timer was going. To do this I believe I need to create a state machine. From my understanding a state machine can't have a delay(). Below is code I created to do start to understand the state machine. This code allows me to input a single digit value between 0-9 and then convert it to a corresponding time frame. The next step I was trying to complete in the above mentioned code was to be able to input a multi digit number i.e. 123456. Again the end goal is to be able to put on##.## or off and then be able to interpret it to get where I need to go. what is the best way to get there from here. It seems like the state machine is good but I don't know how to read the input and interpret it to what I want.
//Created by Jacob
int Led = 13;
unsigned long newMillis =0;
void setup() {
pinMode(Led, OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
if (Serial.available() == 0){
if (currentMillis >= newMillis ){
digitalWrite(Led, LOW);
}
}
else {
int val = Serial.read()-'0';
Serial.print(val);
Serial.println(" inputed value");
newMillis = currentMillis + val*1UL*1000UL;
digitalWrite(Led, HIGH);
}
}
There are many ways to convert a string to a number.
(1) A simple way is using atoi(), if you would like to convert "####.##", you need atoi() twice because atoi() stops at the dot, so another atoi() is needed for numbers after the dot. The data must be read into a buffer, before it can be processed with atoi().
(2) For old style 'C' programmers, there is sscanf. This needs a format, and you have to know how to use it.
(3) The Serial library has a few extra functions, like parseInt().
Using incoming serial data can be placed in a buffer and processed. That would make things easier in the future. But in your first post, you declared the buffer inside an 'if' statement, you better use a global buffer for that.
For now, you can try the parseInt() function.
If something is availabe, use parseInt() to get a number, after that, read the dot with Serial.read(), after that use parseInt() to read the number after the dot.
That should work, but it is far from ideal. Things can go wrong with timeouts, not complete received data, or if another format is sent.
I'm sure someone made a library for this, but I don't know any.
jacob84401:
I sure hope that it is just as easy as you say. Maybe I should give more information on my end goal. I would like to turn on and off electronics through sms messages.
Now that you are more forthcoming, it probably isn't! I think you need ZoomKat more than me.............