what is wrong with my program, error in compiling both transmitter and receiver..
TRANSMITTER
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
#define P1 8
#define P2 3
void setup() {
pinMode(P1, INPUT);
pinMode(P2, INPUT);
mySerial.begin(9600);
}
void loop() {
int buttonState = digitalRead(P1);
int buttonState1 = digitalRead(P2);
if(buttonState == 1){//if button is down
mySerial.println(1234);//send unique code to the receiver in this case 1234
{
if(buttonState1 == 1{
mySerial.println(1234);
}
}
RECEIVER
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#define L1 2
#define L2 3
unsigned long last = millis();//set timer
void setup() {
mySerial.begin(9600);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
}
void loop() {
boolean ledState = digitalRead(L1);//check if the LED is turned on or off. Returns 1 or 0
boolean ledState1 = digitalRead(L2);
if(mySerial.available() > 1){
int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
if(millis() - last > 250){//if time now is 250 milliseconds greater than last time
if(ledState == 0 && input == 1234){//if LED is off and button code is ok
digitalWrite(L1, HIGH);
}else if(ledState == 1 && input == 1234){//if LED is on and button code is ok
digitalWrite(L1, LOW);
{
if(ledState1 == 0 && input == 1234){//if LED is off and button code is ok
digitalWrite(L2, HIGH);
} else if (ledState1 == 1 && input == 1234){
digitalWrite(L2,LOW);
}
}
mySerial.flush();//clear the serial buffer for unwanted inputs
last = millis();//reset timer
}
delay(20);//delay little for better serial communication
}
Your codes are slightly adjusted to balance the {} braces and to come out from compilation error.
Sender codes:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
#define P1 8
#define P2 3
void setup() {
pinMode(P1, INPUT);
pinMode(P2, INPUT);
mySerial.begin(9600);
}
void loop()
{
int buttonState = digitalRead(P1);
int buttonState1 = digitalRead(P2);
if (buttonState == 1)
{ //if button is down
mySerial.println(1234);//send unique code to the receiver in this case 1234
}
/*
{
if(buttonState1 == 1
{
mySerial.println(1234);
}*/
}
Receiver Codes:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
#define L1 2
#define L2 3
unsigned long last = millis();//set timer
void setup()
{
mySerial.begin(9600);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
}
void loop()
{
boolean ledState = digitalRead(L1);//check if the LED is turned on or off. Returns 1 or 0
boolean ledState1 = digitalRead(L2);
if (mySerial.available() > 1)
{
int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
if (millis() - last > 250)
{ //if time now is 250 milliseconds greater than last time
if (ledState == 0 && input == 1234)
{ //if LED is off and button code is ok
digitalWrite(L1, HIGH);
}
else if (ledState == 1 && input == 1234)
{ //if LED is on and button code is ok
digitalWrite(L1, LOW);
}// {
if (ledState1 == 0 && input == 1234)
{ //if LED is off and button code is ok
digitalWrite(L2, HIGH);
} else if (ledState1 == 1 && input == 1234)
{
digitalWrite(L2, LOW);
}
}
mySerial.flush();//clear the serial buffer for unwanted inputs
last = millis();//reset timer
}
delay(20);//delay little for better serial communication
}
I'm keen to hear from @GolamMostafa why he or she commented part of OP's code away- I suppose that's one way of fixing a compiler error, just rem it out.
This code compiles and keeps OP's logic intact. Added 1x ) and 2x }:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
#define P1 8
#define P2 3
void setup()
{
pinMode(P1, INPUT);
pinMode(P2, INPUT);
mySerial.begin(9600);
}
void loop()
{
int buttonState = digitalRead(P1);
int buttonState1 = digitalRead(P2);
if (buttonState == 1)
{ //if button is down
mySerial.println(1234);//send unique code to the receiver in this case 1234
{
if (buttonState1 == 1)
{
mySerial.println(1234);
}
}
}
}
neiklot:
I'm keen to hear from @GolamMostafa why he or she commented part of OP's code away- I suppose that's one way of fixing a compiler error, just rem it out.
That part has appeared to me as redundant; however, the OP may keep it if he thinks it is needed for his sketch.
GolamMostafa:
That part has appeared to me as redundant;
Did you perchance miss the "1" in the second "if"?
if (buttonState == 1)
{ //if button is down
mySerial.println(1234);//send unique code to the receiver in this case 1234
{
if (buttonState1 == 1) //buttonState1 not buttonstate
{
neiklot:
Did you perchance miss the "1" in the second "if"?
if (buttonState == 1)
{ //if button is down
mySerial.println(1234);//send unique code to the receiver in this case 1234
{
if (buttonState1 == 1) //buttonState1 not buttonstate
{
Exactly! I misread and saying now that 'buttonState' and 'buttonState1' are different.