can anybody see why i am getting stuck in a continual loop?
#include <SoftwareSerial.h> //Software lbrary - keeps rx/tx lines open for uploads
SoftwareSerial simc(5, 4);
const int choke_on = 9; // motor driver input
const int choke_off = 8; // motor driver input
pinMode(choke_on, OUTPUT);
pinMode(choke_off, OUTPUT);
void loop() { // loop()
int j = analogRead(A0);
// Serial.println(j);
int DTMF = checkDTMF(); // check incoming DTMF tone
if (DTMF == 1) {
delay(10);
battery_sense();
}
if (DTMF == 3) {
delay(10);
engine_off();
}
if (DTMF == 2) {
delay(10);
parameters();
}
if (DTMF == 8) {
delay(10);
manflag = 1;
Start();
}
if (DTMF == 9) {
delay(10);
choke_in();
}
if (DTMF == 7) {
delay(10);
choke_out();
}
if (DTMF == 5) {
delay(10);
timer_off();
}
if (DTMF == 6) {
delay(10);
timer_up5();
}
if (DTMF == 4) {
delay(10);
timer_up15();
}
}
void simFlush() { // simFlush()
delay(20);
while (simc.available ())
simc.read ();
}
int checkDTMF() { //checkDTMF()
if (simc.find("+DTMF:")) {
return simc.parseInt();
simFlush();
}
}
void choke_in() {
digitalWrite(choke_off, LOW);
digitalWrite(choke_on, HIGH); // Fully close the choke
delay(500); // Give servo time to act
digitalWrite(choke_on, LOW);
delay(2000);
v(10); delay (1000); // "CHOKE"
v(11); delay (1200); // "ON"
Serial.println("choke in");
}
if i call any of the functions they will just continuously repeat, is this something to do with the way i am calling the functions (using IF statements?).
even if i include the flush at the end of a function it still repeats?
Robin2:
You need to post the complete program so we can see how everything is defined.
EDITED
Robin2:
The flush() function does not empty the input buffer - it is intended to block the Arduino until all the outgoing data is sent.
Sorry i was not implementing the flush() function. What i meant to say is that even if i try to flush the buffer with:
while (simc.available ())
simc.read ();
at the end of the function the it still repeats,
at this moment i'm looking at the line ending, but thought that the buffer would be cleared through the simc.read()....
could i then use int x = receivedChars[7]; to isolate the key(DTMF) press?
Nearly, it needs to be
int xVal = receivedChars[7] - '0';
receivedChars[7] will have the Ascii value 49. Subtracting the Ascii value for '0' will give you the value 1
And it is a bad idea to use single character variable names - they can be impossible to find with search or search and replace. Always use meaningful variable names.
receivedChars[7] will have the Ascii value 49. Subtracting the Ascii value for '0' will give you the value 1
And it is a bad idea to use single character variable names - they can be impossible to find with search or search and replace. Always use meaningful variable names.
...R
OK thanks robin for the help, rather than doing the math ill just get it to look for the ascii values instead, this way i can incorporate the # and * keys as below:
#include <SoftwareSerial.h>
SoftwareSerial simc(5, 4);
const byte numChars = 12;
char receivedChars[numChars];
bool newData = 0;
char s;
void setup() {
Serial.begin(9600);
simc.begin(9600);
Serial.println("starting");
delay(3000);
simc.println("ATS0=1");
delay(3000);
simc.println("AT+DDET=1,1000,0,0");
delay(3000);
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (simc.available() > 0 && newData == false) {
rc = simc.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
int s = receivedChars[7];
newData = false;
if (s == 35) {
Serial.println("#########");
}
if (s == 48) {
Serial.println("000000000");
}
if (s == 42) {
Serial.println("***********");
}
if (s == 51) {
Serial.println("333333333");
}
if (s == 52) {
Serial.println("444444");
}
if (s == 53) {
Serial.println("5555");
}
if (s == 54) {
Serial.println("6666666666");
}
if (s == 55) {
Serial.println("777777777");
}
if (s == 56) {
Serial.println("88888888");
}
if (s == 57) {
Serial.println("99999999");
}
if (s == 50) {
Serial.println("222222222222");
}
if (s == 49) {
Serial.println("1111111111111111");
delay(3000);
Serial.println("done");
}
}
}
However each subsequent key press will repeat the function, any key press after the first will print the old value first and then the new value: