Pages: [1]   Go Down
Author Topic: arduino BT and serial commands  (Read 428 times)
0 Members and 1 Guest are viewing this topic.
berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 154
fubbi.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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);
    }    
    
  }
}
« Last Edit: March 11, 2010, 02:21:32 pm by fubbi » Logged

berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 154
fubbi.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thank you

fubbi
Logged

berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 154
fubbi.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

help?
Logged

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 324
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
« Last Edit: March 13, 2010, 09:30:30 am by RoyK » Logged

berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 154
fubbi.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Pages: [1]   Go Up
Jump to: