hi
i my application i have two arduino used as master and slave.
when my led on slave arduino turns on it has to send message to master arduino.
i am able to achieve this but the problem is i want the message to be sent only once,like once led is on send message then stop.
rs485 is used for message transmission.
What I have tried:
master code
#include <softwareserial.h>
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
int ledpin = 13;
int data;
int data1;
int ack = 0;
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);
int byteReceived;
int byteSend;
void setup()
{
Serial.begin(9600);
Serial.println("SerialRemote"); pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive);
RS485Serial.begin(9600); pinMode(ledpin,OUTPUT);
digitalWrite(ledpin,LOW);
}
void loop()
{
if (RS485Serial.available())
{
digitalWrite(ledpin,HIGH);
byteReceived = RS485Serial.read(); Serial.write(byteReceived); delay(15);
digitalWrite(SSerialTxControl, RS485Transmit);
}
else{}
digitalWrite(SSerialTxControl, RS485Receive);
}
slave code
#include <softwareserial.h>
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 4 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); int byteReceived;
int byteSend;
volatile unsigned long LastPulseTimeA;
volatile unsigned long LastPulseTimeB;
int durationA;
int durationB;
int distanceA;
int distanceB;
#define trigPinA 7
#define echoPinA 2
#define trigPinB 8
#define echoPinB 3
#define ledpinA 13
#define ledpinB 12
int val = 1;
int val1 = 1;
int temp = 0;
int temp1 = 0;
int slave1=1;
int slave2 = 2;
void setup() {
Serial.begin (9600);
pinMode(trigPinA, OUTPUT);
pinMode(echoPinA, INPUT);
pinMode(trigPinB, OUTPUT);
pinMode(echoPinB, INPUT);
pinMode(ledpinA,OUTPUT);
pinMode(ledpinB,OUTPUT);
attachInterrupt(0, EchoPinA_ISR, CHANGE); attachInterrupt(1, EchoPinB_ISR, CHANGE); digitalWrite(ledpinA,LOW);
digitalWrite(ledpinB,LOW);
Serial.begin (9600);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Transmit); RS485Serial.begin(9600);
}
void loop(){
digitalWrite(trigPinA, LOW);
digitalWrite(trigPinB, LOW);
delayMicroseconds(2);
digitalWrite(trigPinA, HIGH);
digitalWrite(trigPinB, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinA, LOW);
digitalWrite(trigPinB, LOW);
if (distanceA<10)
{
Serial.print("Sensor A ");
Serial.print(distanceA);
Serial.print('\t');
digitalWrite(ledpinA,HIGH);
}
else{
digitalWrite(ledpinA,LOW);
}
if (distanceB<10)
{
Serial.print("Sensor B ");
Serial.print(distanceB);
Serial.print('\t');
digitalWrite(ledpinB,HIGH);
}
else{
digitalWrite(ledpinB,LOW);
}
temp = digitalRead(ledpinA);
if(temp==val){
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.write("car A present ");
digitalWrite(SSerialTxControl, RS485Receive);
}
else{}
digitalWrite(SSerialTxControl, RS485Transmit);
}void EchoPinA_ISR() {
static unsigned long startTimeA;
if (digitalRead(2)) startTimeA = micros();
else LastPulseTimeA = micros() - startTimeA;
distanceA=(LastPulseTimeA/2) / 29.1,1;
}
void EchoPinB_ISR() {
static unsigned long startTimeB;
if (digitalRead(3)) startTimeB = micros();
else LastPulseTimeB = micros() - startTimeB;
distanceB=(LastPulseTimeB/2) / 29.1,1;
}