Hey guys so here is my problem. My sketch is set to receive commands from a Bluetooth app i created with AI2. I also have a push button witch will do the same just as a backup. Everything works as advertised I just cant figure out how to get my app to reflect the current state of my device if the push button is used. For example if i turn it on with the app then off with the button my app still reflects as on. Any help would be greatly appreciated. I also attached my block setup in AI2
boolean debug = true;
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
// Connect the HC-06 TX to the Arduino RX.
// Connect the HC-06 RX to the Arduino TX
// max length of command is 20 chrs
const byte numChars = 20;
char receivedChars[numChars];
boolean newData = false;
// constants won't change.
const int downbutton = 2; // downbutton to pin 2
const int upbutton = 3; // upbutton to pin 3
const int relay1 = 7; // relay1 to pin 7
const int relay2 = 6; // relay2 to pin 6
const int relay3 = 5; // relay3 to pin 5
const int relay4 = 4; // relay4 to pin 4
const int accpi = 8; // car acc power in to pin 8
// variables will change:read for input values
int buttonState2 = 0;
int buttonState3 = 0;
int accps = 0;
int relay4s = 0;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
BTserial.begin(57600);
pinMode(accpi, INPUT);
pinMode(upbutton, INPUT); // sets the downbutton pin as input
pinMode(downbutton, INPUT); // sets the downbutton pin as input
pinMode(relay1, OUTPUT); // sets the relay pin as output
pinMode(relay2, OUTPUT); // sets the relay pin as output
pinMode(relay3, OUTPUT); // sets the relay pin as output
pinMode(relay4, OUTPUT); // sets the relay pin as output
pinMode(13, OUTPUT); // set pin 13 as an output so we can use LED to monitor sleep status
digitalWrite(relay1, LOW); // Prevents relays from starting up engaged
digitalWrite(relay2, LOW); // Prevents relays from starting up engaged
digitalWrite(relay3, LOW); // Prevents relays from starting up engaged
digitalWrite(relay4, LOW); // Prevents relays from starting up engaged
}
void loop() {
digitalWrite(13, HIGH); // turn pin 13 LED on
buttonState3 = digitalRead(upbutton); // read the state of the upbutton
buttonState2 = digitalRead(downbutton); // read the state of the downbutton
accps = digitalRead(accpi); // read the state of the accps
relay4s = digitalRead(relay4);
if (accps == HIGH && buttonState2 == HIGH && relay4s == LOW) {
// turn relay 3/4 on w/ relay 1 on for 16 sec:
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay1, HIGH);
delay(5000);
}
if (accps == HIGH && buttonState3 == HIGH && relay4s == HIGH) {
// turn relay 3 off w/ relay 2/4 on for 16 sec then off:
digitalWrite(relay3, LOW);
digitalWrite(relay2, HIGH);
delay (5000);
digitalWrite(relay4, LOW);
}
if (accps == LOW && relay4s == HIGH) {
// when accp is off for 3 mins turn relay 3 off w/ relay 2/4 off after 16 secs:
delay(5000); //add0
digitalWrite(relay3, LOW);
digitalWrite(relay2, HIGH);
delay(5000);
digitalWrite(relay4, LOW);
}
if (buttonState2 == LOW || buttonState3 == LOW) {
// sets relay 1/2 to off if no switches are pressed:
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
if (BTserial.available() > 0) {
recvWithStartEndMarkers();
}
if (newData) {
parseData();
}
if (accps == LOW) {
// Stay awake for 1 second, then sleep.
delay(1000);
sleepNow();
}
}
void parseData()
{
newData = false;
if (debug) {
Serial.println( receivedChars );
}
if (receivedChars[0] == 'O' && receivedChars[1] == 'N' && accps == HIGH && relay4s == LOW ) {
// turn relay 3/4 on w/ relay 1 on for 16 sec:
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay1, HIGH);
delay(5000);
BTserial.print("ON");
}
if (receivedChars[0] == 'O' && receivedChars[1] == 'F' && accps == HIGH && relay4s == HIGH ) {
digitalWrite(relay3, LOW);
digitalWrite(relay2, HIGH);
delay (5000);
digitalWrite(relay4, LOW);
}
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
if (BTserial.available() > 0)
{
rc = BTserial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void rupt(void) {
// just wake from sleep
}
void sleepNow(void) {
// Choose our preferred sleep mode
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Enable interrupt
attachInterrupt(0, rupt, HIGH);
// pin 2
attachInterrupt(1, rupt, HIGH);
// pin 3
// Set sleep enable (SE) bit:
sleep_enable();
digitalWrite(13, LOW); // turn LED off to indicate sleep
// Put the device to sleep:
sleep_mode();
// Upon waking up, sketch continues from this point.
sleep_disable();
}