Pages: [1]   Go Down
Author Topic: Serial Read with Switch/Case problem  (Read 990 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
  I am using an UNO board with a Bluesmirf BT module connected to an android app.
I am trying to get the program to read the serial input from the app through the BlueSmirf
and run a different case depending on the input.

For some reason it does nothing when I press an input in the app and I can't work out why.

For a test I have an LED hooked up to pin 13.  I can get it to switch on and off using the app with
no problems with the first set of code.

however, I have tried to integrate that into the second set of code and I don't think I am doing it right
and possibly have the serial.read in the wrong place.

I figured if I can get case 4 (test LED)to work then I can get the rest of the code to work for my device.

can anyone see what I am doing wrong?

The android app says the following:

microController BT outputs standard 2 byte messages. The messages contain a key (0-255) and a value (0-255).

outgoing: (key, value)

(Working bit)
Code:
const int ledpin = 13;

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (TestLED)
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
    if( Serial.available() >= 2)       // if data is available to read
  {
    int key = Serial.read();
    int val = Serial.read();
    digitalWrite (key,val);
(Not Working Code)
Code:
const int ShakePin = 4;
const int FeedPin = 5;
const int StopPin = 6;
const int TestPin = 13;
byte mode;



void setup()
{
  pinMode(ShakePin, OUTPUT);  //Shaker Relay Pin
  pinMode(ShakePin, LOW);  //
  pinMode(FeedPin, OUTPUT);  // Feeder Relay Pin
  pinMode(FeedPin, LOW);  //
  pinMode(StopPin, INPUT);  // Cutout Switch
  pinMode(StopPin, HIGH);  //
 
  pinMode(TestPin, OUTPUT);  // pin 12 shakermotor as OUTPUT
  pinMode(TestPin, LOW);  // pin 12 shakermotor as OUTPUT
 
  Serial.begin(115200);       // start serial communication at 115200bps
 
}
 
void loop() {
  if( Serial.available() >= 2){
   
    int key = Serial.read();
    int val = Serial.read();
     
   
    if ( mode == 0 && key == 4){ mode = 1;}
    if ( mode == 0 && key == 5){ mode = 2;}
    if ( mode == 0 && key == 13){ mode = 4;}
   
    switch (mode){
   
case 0:  // do nothing and wait
   break;

case 1:  // Shake Relay
    digitalWrite (ShakePin, HIGH);
    delay (5000);
    digitalWrite (ShakePin, LOW);
    mode = 0;
   break;

case 2:  // Feed Relay
    digitalWrite (ShakePin, HIGH);
    delay (5000);
    digitalWrite (ShakePin, LOW);
    digitalWrite (FeedPin, HIGH);
    delay (3000);
    digitalWrite (FeedPin, HIGH);
    if (digitalRead(StopPin) == LOW) mode = 3;
break;
   
case 3:  //Stop Feed Relay and GoTo mode 0
    digitalWrite (FeedPin, LOW);
    mode = 0;
break;

case 4:   //LED Test
     digitalWrite (TestPin,HIGH);
     delay (3000);
     digitalWrite (TestPin,LOW);
     mode = 0;
     
  }
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is sending the data? Is the data binary or ASCII? Your non-working code assumes binary, but that may not be the case.

Every case, even the last one, should have a break statement.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The android app sends the data over bluetooth to the serial port when you press a touchscreen button.
The app sends out two byte messages. I am trying to interpret the first byte (key) into different variables.
On the app you can set different buttons to output different key bytes. It also outputs a value for each key
as well in the second byte but I am not using that.

I added the break; to the last case but still no joy. How do I find out if the output is ASCII or binary? does the fact that
it works in the first set of code indicate anything?

« Last Edit: December 05, 2012, 03:46:21 pm by flotilla » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to help us understand what "it doesn't work" means. Do the wrong pins go HIGH/LOW?

Code:
  pinMode(ShakePin, OUTPUT);  //Shaker Relay Pin
  pinMode(ShakePin, LOW);  //
  pinMode(FeedPin, OUTPUT);  // Feeder Relay Pin
  pinMode(FeedPin, LOW);  //
  pinMode(StopPin, INPUT);  // Cutout Switch
  pinMode(StopPin, HIGH);  //
Let's see. Is HIGH equal to INPUT or OUTPUT? I'm guessing that the second statement in each pair is supposed to be digitalWrite(), not pinMode().
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh, I have been staring at this code for two hours and missed that every time. Damn it.

Changed the relevant lines to digitalWrite and it now works great.  Thanks again Paul! I
Can now get on with the mechanical side of things.

Thanks
Logged

Pages: [1]   Go Up
Jump to: