Two examples of capturing a string of numeric characters from the serial port and generating a number.
// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(2000); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n;
char carray[6]; //converting string to number
readString.toCharArray(carray, sizeof(carray));
n = atoi(carray);
myservo.writeMicroseconds(n); // for microseconds
//myservo.write(n); //for degees 0-180
readString="";
}
}
// zoomkat 10-14-11 serial servo test
// use a microseconds value like 1500 in serial monitor
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("servo-test-22"); // 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); // allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
myservo.writeMicroseconds(readString.toInt()); //convert readString to number for servo
readString=""; //empty for next input
}
}
You're declaring three buffers, all with the same name, and one of them is declared to have no storage, so won't hold the 1 you've told it to.
That will never work.
What are you trying to do?
I try to resolve the problem for input sequence "001".
Code:
#include <VirtualWire.h>
char c;
char buf1[5];
int i=0;
int t;
unsigned flag=0;
void setup(){
Serial.begin(9600);
vw_set_tx_pin(4);
vw_setup(1000);
}
void loop(){
while (Serial.available()>0) {
t = Serial.read();
buf1[i]=(t-48);//ascii conversion
i++;
flag=1;
delay(2); // allow buffer to fill with next character
}
if (flag==1) {
Serial.println(buf1);//see the buffer
i=0;
flag=0;
int n1=int(buf1[0]);//Debug only
Serial.print(n1);
int n2=int(buf1[1]);
Serial.print(n2);
int n3=int(buf1[2]);
Serial.print(n3);
int n4=int(buf1[3]);
Serial.print(n4);
int n5=int(buf1[4]);
Serial.print(n5);
vw_send((uint8_t *)buf1,5);
}
}
Take a look because i dont know if its right.Its works but when i input the first sequence to the port its makes a empy line and next print the buf1.Why?
Thank you for the help.
I want to put on buf1[1] a sum of numbers that i have input on serial port but happens i problem very strange for me.
This is the code:
#include <VirtualWire.h>
char buf1[11];
int i=2;
int t;
unsigned flag=0;
void setup(){
Serial.begin(9600);
vw_set_tx_pin(4);
vw_setup(1000);
}
void loop(){
while (Serial.available()) {
t = Serial.read();
//Serial.print("aqui"); //gets one byte from serial buffer
buf1[i]=(t-48);
i++;
flag=1;
delay(2); // allow buffer to fill with next character
}
if (flag==1) {
//Serial.println(buf1);
i=2; //so you can see the captured string
flag=0;
//Serial.println("where");
buf1[0]=1;
buf1[1]=checksum();
buf1[10]=1;
int n1=int(buf1[0]);
Serial.print(n1);
int n2=int(buf1[1]);
Serial.print(n2);
int n3=int(buf1[2]);
Serial.print(n3);
int n4=int(buf1[3]);
Serial.print(n4);
int n5=int(buf1[4]);
Serial.print(n5);
int n6=int(buf1[5]);
Serial.print(n6);
int n7=int(buf1[6]);
Serial.print(n7);
int n8=int(buf1[7]);
Serial.print(n8);
int n9=int(buf1[8]);
Serial.print(n9);
int n10=int(buf1[9]);
Serial.print(n10);
int n11=int(buf1[10]);
Serial.println(n11);
vw_send((uint8_t *)buf1,11);
}
}
int checksum(){
unsigned long sum=0;
int i1;
for(i1=9;i1>=2;i1--){
sum+=int(buf1[i]);
int n=int(buf1[i]);
Serial.println(n);
}
return sum;
}
The problem is when i input zeros,for example:
I input"11111111" an the int checksum() function prints the buffer the same number that the loop function but when a input"20000000" checksum prints wrong"22222222".