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)
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)
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;
}
}
}