I am reading a text fie to activate a switch using integers in the text. The problem is my text file has letters and the integers.
eg
test : 4
I have written the code attached to only return the first integer it reads.
int serInLen=25;
char serInString[25];
bool fileOk=false;
int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() // run once, when the sketch starts
{
pinMode(buttonPin , INPUT); // sets the digital pin as output
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
delay(3000);
}
void loop() // run over and over again
{
char buffer[5];
int pinAct=0;
int lineNr=0;
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
for(lineNr=1;lineNr<=1;lineNr++) {
Serial.print("#S|RDPIN|[");
Serial.print(itoa((lineNr), buffer, 10));
Serial.println("]#");
// Wait for answer from Gobetwino and convert the returned answer to an integer, and call the blink function
// An error check for a negative return code should be done here, but is ommited to keep the example as simple as posible
readSerialString (serInString, 10000);
int i =0;
while((serInString[i] < "0") && (serInString[i] >"9"))
i++;
pinAct = serInString [i] - "0";
pinAct=atoi(serInString);
Button (pinAct);
delay(5000);
}
}
void Button(int times) {
int x;
for(x= 1; x<=times; x++) {
digitalWrite(buttonPin, HIGH);
delay(200);
}
}
void readSerialString (char *strArray,long timeOut) {
// Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
long startTime=millis();
int i;
while (!Serial.available()) {
if (millis()-startTime >= timeOut) {
return;
}
}
while (Serial.available() && i < serInLen) {
strArray[i] = Serial.read();
i++;
}
}
The while loop below is giving the (Iso C++ forbids comparison between pointer and interger) error
int i =0;
while((serInString[i] < "0") && (serInString[i] >"9"))
i++;
pinAct = serInString [i] - "0";
buttons.ino (1.84 KB)