What i want to do is monitor state of pin 7 when pinstate is high i will serially give command to on/off led on pin 13.
I give High to pin 7 from +5V pin that is why its configured as input.
To on led i enter "#A.on*"
To off led i enter "#A.off*"
I have added here1,2,3,4,5... to track execution of code.
The pin state detection is working and if i first give command to ON led it glows if i give OFF then it remains off.
Now the problem is it only works for 1st command then not. if i give ON first after it even if i give OFF command it will go through HERE1 loop and not turn off. Likewise if i first OFF it after it will not glow even if the command is of ON it will go through HERE5 loop.
I think maybe problem is with temp,
#define led 13
int temp = 0, i = 0;
char str[15];
int pinstate = 0;
int lastpinstate = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(7, INPUT);
digitalWrite(13, HIGH);
delay(3000);
digitalWrite(13, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
pinstate = digitalRead(7);
if (pinstate != lastpinstate)
{
if (digitalRead(7) == HIGH)
{
Serial.println("Pinstate High");
if (temp == 0)
{
serialEvent();
temp = 0;
i = 0;
Serial.println("Here2");
delay(1000);
}
}
else
{
Serial.println("Pinstate Low");
}
}
lastpinstate = pinstate;
}
void serialEvent()
{
while (Serial.available())
{
if (Serial.find("#A."))
{
delay(1000);
while (Serial.available())
{
char inChar = Serial.read();
str[i++] = inChar;
if (inChar == '*')
{
check();
temp = 1;
return;
}
}
}
}
}
void check()
{
Serial.println("Here3");
if (!(strncmp(str, "on", 2)))
{
Serial.println("Here1");
digitalWrite(13, HIGH);
delay(200);
}
else if (!(strncmp(str, "off", 3)))
{
Serial.println("Here5");
digitalWrite(13, LOW);
delay(200);
}
}