I have removed the hard-coded delays from the code and modified it quite a bit. Still, the last communication is not happening. I have posted both the codes.
//Code for arduino 2
#include <SoftwareSerial.h>
#include <RS485_non_blocking.h>
#include <RS485_protocol.h>
SoftwareSerial rs485(10,11); //receive pin,transmit pin
//variable definitions
const int ENABLE_PIN=12; //RS485 communication control pin
void setup() {
// put your setup code here, to run once:
rs485.begin(28800);
pinMode(ENABLE_PIN,OUTPUT);
Serial.begin(9600);
}
//RS485 protocol
void fWrite(const byte what){
rs485.write(what);
}
int fAvailable(){
return rs485.available();
}
int fRead(){
return rs485.read();
}
//RS485 protocol ends
void loop() {
// put your main code here, to run repeatedly:
byte jumbo[]={0,65,135}; //address,lower range,higher range -set accordingly
byte request[10];
byte requestreceived=recvMsg(fAvailable,fRead,request,sizeof(request));
if(requestreceived){
Serial.println("Wakey Wakey");
int i;
for(i=0;i<3;i++){
Serial.println(request[i]);
}
delay(1); //Change wait time accordingly during presentation
digitalWrite(ENABLE_PIN,HIGH);
sendMsg(fWrite,jumbo,sizeof(jumbo));
delayMicroseconds(660);
digitalWrite(ENABLE_PIN,LOW);
Serial.println("Reciprocated");
}
}
//code for arduino 1
#include <RS485_non_blocking.h>
#include <RS485_protocol.h>
#include <SoftwareSerial.h>
//Pin Initialisation
SoftwareSerial rs485(10,11); //receive pin, transmit pin
SoftwareSerial mySerial(8,9);
//variable definitions
const int ENABLE_PIN=12; // RS485 communication control pin
const int Triggerpin=3;
const int redpin=4;
const int greenpin=5;
boolean speeddatareceived=false;
volatile boolean triggered=false;
boolean communicationon=false;
int speeddata;
unsigned long int timeon;
unsigned long int interval;
//Rs485 protocol begins
void fWrite(const byte what){
rs485.write(what);
}
int fAvailable(){
return rs485.available();
}
int fRead(){
return rs485.read();
}
//RS485 protocol ends
//wait function
void wait(int interval){
unsigned long previousmillis=millis();
unsigned long currentmillis;
while(1>0){
currentmillis=millis();
if((currentmillis-previousmillis)>=interval)break;
}
return;
}
void timeout(){
triggered=false;
communicationon=false;
speeddatareceived=false;
Serial.println("Assimilator not responding.Communication aborted");
delay(100);
}
//ISR
void trigger(){
triggered=true;
}
void setup() {
// put your setup code here, to run once:
rs485.begin(28800);
pinMode(ENABLE_PIN,OUTPUT);
pinMode(redpin,OUTPUT);
pinMode(greenpin,OUTPUT);
Serial.begin(9600);
mySerial.begin(28800);
//pinMode(Triggerpin,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(3),trigger,FALLING); //check both high and rising to see which one works
}
void loop() {
// put your main code here, to run repeatedly:
if((triggered==true)&&(communicationon==false)){
Serial.println("Ready");
//delay(100); //wait for 100 ms for first arduino to process
if(mySerial.available()>0){
speeddata=mySerial.read();
Serial.println(speeddata);
//delay(100);
if(speeddata>0)speeddatareceived=true;
}
else {Serial.println("False alarm");
triggered=false;
speeddatareceived=false;
}
//time to request geophone assimilator for data
byte request[]={0,1,0};
if(speeddatareceived==true){
digitalWrite(ENABLE_PIN,HIGH);
sendMsg(fWrite,request,sizeof (request));
delayMicroseconds(660);
digitalWrite(ENABLE_PIN,LOW);
Serial.println("Message sent");
communicationon=true; //flag variable
timeon=millis();
}
}
//receive response
byte message[10];
byte received=recvMsg(fAvailable,fRead,message,sizeof(message));
if((received)&&(communicationon==true)){
// put in main response
Serial.println("Action");
triggered=false;
speeddatareceived=false;
communicationon=false;
}
if((communicationon==true)&&(received==0)){
unsigned long int timenow=millis();
interval= timenow-timeon;
if(interval>=10000)timeout(); //wait for 10 seconds for assimilator to respond, otherwise terminate communication
}
}