Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
#include <softwareserial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#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;
/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("SerialRemote");  // Can be ignored
  pinMode(SSerialTxControl, OUTPUT);  
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver
  
  // Start the software serial port, to another device
  RS485Serial.begin(9600);   // set the data rate 
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin,LOW);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  //Copy input data to output  
  if (RS485Serial.available()) 
  {
    digitalWrite(ledpin,HIGH);
    byteReceived = RS485Serial.read();   // Read the byte 
    Serial.write(byteReceived);         // Show on Serial Monitor
  delay(15);
   digitalWrite(SSerialTxControl, RS485Transmit);
    }
    else{}
  digitalWrite(SSerialTxControl, RS485Receive); 
      //delay(100);
       //digitalWrite(SSerialTxControl, RS485Transmit);
      }
      
     /*if(byteReceived == 2){
        digitalWrite(SSerialTxControl, RS485Receive);
      data1 = RS485Serial.read();
      Serial.write(data1); 
       digitalWrite(SSerialTxControl, RS485Transmit);
        }
        else {};
        
   delay(100);
  // End If RS485SerialAvailable
   digitalWrite(SSerialTxControl, RS485Receive);
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//NONE

//*********( THE END )***********



slave code

C++
#include <softwareserial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#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); // RX, TX
int byteReceived;
int byteSend; 

volatile unsigned long LastPulseTimeA;
volatile unsigned long LastPulseTimeB;
int durationA;
int durationB;
int distanceA;
int distanceB;
//unsigned long startTime;
#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);  // Pin 2 interrupt on any change
attachInterrupt(1, EchoPinB_ISR, CHANGE);  // Pin3 interrupt on any change
digitalWrite(ledpinA,LOW);
digitalWrite(ledpinB,LOW);

Serial.begin (9600);
pinMode(SSerialTxControl, OUTPUT); 
 digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit 
 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&lt;10)
   {
    Serial.print("Sensor A  ");
    Serial.print(distanceA);
    Serial.print('\t');
    digitalWrite(ledpinA,HIGH);
    
  }
   else{
    digitalWrite(ledpinA,LOW);
    }

    if (distanceB&lt;10)
  {
     Serial.print("Sensor B  ");
    Serial.print(distanceB);
    Serial.print('\t');
    digitalWrite(ledpinB,HIGH);
    
  }
   else{
    digitalWrite(ledpinB,LOW);
    }

  temp = digitalRead(ledpinA);
  //temp1 = digitalRead(ledpinB);
  if(temp==val){
     
      digitalWrite(SSerialTxControl, RS485Transmit);
      RS485Serial.write("car A present  ");
       digitalWrite(SSerialTxControl, RS485Receive); 
      }
      else{}
     
      digitalWrite(SSerialTxControl, RS485Transmit);
   
//  if(temp1==val1){
     
      //digitalWrite(SSerialTxControl, RS485Transmit);
      //RS485Serial.write("car B present  ");
      // digitalWrite(SSerialTxControl, RS485Receive); 
     // }
     // else{}
      // digitalWrite(SSerialTxControl, RS485Transmit);
 }//loop end
void EchoPinA_ISR() {
    static unsigned long startTimeA;
   
    if (digitalRead(2)) // Gone HIGH
        startTimeA = micros();
    else  // Gone LOW
          LastPulseTimeA = micros() - startTimeA;
          distanceA=(LastPulseTimeA/2) / 29.1,1;
}
void EchoPinB_ISR() {
    static unsigned long startTimeB;
   
    if (digitalRead(3)) // Gone HIGH
        startTimeB = micros();
    else  // Gone LOW
        LastPulseTimeB = micros() - startTimeB;
        distanceB=(LastPulseTimeB/2) / 29.1,1;
}
Posted
Updated 13-May-16 7:54am
v2

In this code val is set to 1 so that every time the ledPinA goes HIGH you will repeatedly send the "car A present" message, unless I'm missing something here. To over come this set a flag when you 1st send the message and set the flag when the ledPinA goes HIGH and reset when it goes LOW then check the flag in your code.
C++
temp = digitalRead(ledpinA);
//temp1 = digitalRead(ledpinB);
if(temp==val && !flag){
    ...
    flag = true;
 } else {
    flag = false;
 }
 
Share this answer
 
Change pin
Try Pin3 with interrupt
#define SSerialTxControl 3

Please check with interrupt, micro, millis, delay() does nor work!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900