Go Down

Topic: arduino BT and serial commands (Read 574 times) previous topic - next topic

fubbi

Mar 11, 2010, 07:24 pm Last Edit: Mar 11, 2010, 08:21 pm by fubbi Reason: 1
Hi

I am getting weird results with this code. If I send an "a" I get a response, but only once. Sending a "d" freezes the serial monitor. If hit "b" after "a" nothing happens, but no character works after that.

thanks for help

Code: [Select]


int ledPin =13;

int SOLENOID1 = 5;
int SOLENOID2 = 7;
int SOLENOID3 = 10;
int SOLENOID4 = 12;

int solenoids[] = {SOLENOID1,SOLENOID2,SOLENOID3,SOLENOID4};
int cnt = 0;

void setup()
{
 Serial.begin(115200); //init of the serial com
 pinMode(ledPin, OUTPUT);
 for(int i=0;i<4;i++){
   pinMode(solenoids[i], OUTPUT);
 }
}

void loop()
{
 
 cnt++;
 cnt %= 20;
 if(cnt < 10) digitalWrite(ledPin, LOW);
 else digitalWrite(ledPin, HIGH);
 
 
 for(int i=0;i<4;i++){
   digitalWrite(solenoids[i], LOW);
 }
 delay(50);
 checkInput();
 delay(50);
 //Serial.println("loop");
}

void checkInput(){
//  boolean hadInput = false;
 if ( Serial.available()) { // serial reception
   char ch = Serial.read();
   int solenoidID = -1;
   switch(ch) {
   case 'a': // numeric value
     solenoidID = 0;
     break;
   case 'b': // reading of the letter
     solenoidID = 1;
     break;
   case 'c':
     solenoidID = 2;
     break;
   case 'd':
     solenoidID = 3;
     break;
   }
   
   if(solenoidID > -1){
     Serial.print("SOLENOID : ");
     Serial.println(solenoidID);
     digitalWrite(solenoids[solenoidID], HIGH);
   }    
   
 }
}


fubbi

Is it possible that the serial data needs to be flushed somehow?

thank you

fubbi


RoyK

#3
Mar 13, 2010, 03:24 pm Last Edit: Mar 13, 2010, 03:30 pm by RoyK Reason: 1
Works for me. (v16)
I copied and pasted your code and ran it on a Mega

Here's what my serial monitor outputs when I type a<Enter> b<Enter> c<Enter> d<Enter> a couple of times

SOLENOID : 0
SOLENOID : 1
SOLENOID : 2
SOLENOID : 3
SOLENOID : 0
SOLENOID : 1
SOLENOID : 2
SOLENOID : 3

Maybe your baud setting for the serial monitor is incorrect?

(You really should add a default: case to your switch statement to handle characters other than a-d even if it's just to ignore them.)

fubbi

so the code is okay i guess? It must be something related to the bluetooth and sending serial.

Is there any way to make the way I am working with the serial more folproof or safe?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy