for some reason the pins only read when then nano first powers up and then it writes the RS485 serial data as it's first read but then it will not change when the program loops.. I've checked the voltages on pins. everything is functioning properly but the arduino is not executing the if statements
HighSpeed_clientSlave_v1_wBoschSensor_A-u-1_v2.ino (7.84 KB)
the arduino is not executing the if statements
Print the values that you are testing before the test ?
Do they look reasonable ?
#include "Arduino.h"
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#define SEALEVELPRESSURE_HPA (1013.25)
#define motion 0
#define ledPin 13
#define LIGHTSENSORPIN A3
// These are the pins we'll be talking to the RS485 device on
#define RS485rx 10 // RS485 Receive pin 7
#define RS485Tx 11 // RS485 Transmit pin 6
#define RS485inout 12 // RS485 Transmit or Receive status
#define RS485Transmit HIGH
#define RS485Receive LOW
#define baudRate 9600
//----BME---------------------------------------------//
#define SEALEVELPRESSURE_HPA (1013.25)
int F_temperature;
Adafruit_BME680 bme; // I2C
//---RS485---------------------------------------------//
SoftwareSerial RS485(RS485rx, RS485Tx);
// bytes we're sending or receiving
byte rxValue;
byte txValue;
byte analog = 0;
//---RS485---------------------------------------------//
int slaveClient_ID = 1;
int proxMotion;
int inMotion;
int outMotion;
int LumenValue;
int gasSensor_Alarm;
int gasSensor_Heartbeat;
int gasSensor_Heartbeat_Fault;
int inMotion_LED;
int outMotion_LED;
int proxMotion_LED;
int gasSensor_Alarm_LED;
int gasSensor_Heartbeat_LED;
int buzzerAlarm;
int Alarm_Status;
int lightLevel;
int NUL = 0;
// -----------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP
// -----------------------------------------------------------------
void setup()
{
Serial.begin(9600);
while (!Serial);
if (!bme.begin()) {
while (1);
}
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
//---------------------------------------
pinMode(A0, INPUT);
proxMotion = digitalRead(A0);
//---------------------------------------
pinMode(A1, INPUT);
inMotion = digitalRead(A1);
//---------------------------------------
pinMode(A2, INPUT);
outMotion = digitalRead(A2);
//---------------------------------------
pinMode(3, INPUT);
gasSensor_Alarm = digitalRead(3);
//---------------------------------------
pinMode(2, INPUT);
gasSensor_Heartbeat = digitalRead(2);
//---------------------------------------
inMotion_LED = 0;
outMotion_LED = 0;
proxMotion_LED = 0;
gasSensor_Alarm_LED = 0;
gasSensor_Heartbeat_LED = 0;
gasSensor_Heartbeat_Fault = 0;
buzzerAlarm = LOW;
lightLevel = 0;
Alarm_Status = 0;
pinMode(LIGHTSENSORPIN, INPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(RS485inout, OUTPUT);
RS485.begin(baudRate);
}
// -----------------------------------------------------------------//
void loop()
{
Serial.flush(); //flush all previous received and transmitted data
digitalWrite(RS485inout, RS485Receive);
while((!RS485.available()) || (RS485.read() != 'u'))
{
}
digitalWrite(RS485inout, RS485Transmit);
delayMicroseconds(100);
{
digitalWrite(ledPin, HIGH);
RS485.write(slaveClient_ID);
delayMicroseconds(5);
RS485.write(proxMotion);
delayMicroseconds(5);
RS485.write(inMotion);
delayMicroseconds(5);
RS485.write(outMotion);
delayMicroseconds(5);
RS485.write(gasSensor_Alarm);
delayMicroseconds(5);
RS485.write(gasSensor_Heartbeat_Fault);
delayMicroseconds(5);
RS485.write(Alarm_Status);
delayMicroseconds(5);
RS485.write(F_temperature);
delayMicroseconds(5);
RS485.write(bme.humidity);
delayMicroseconds(5);
RS485.write(bme.pressure -60);
delayMicroseconds(5);
RS485.write(bme.gas_resistance*100 / 1000000);
delayMicroseconds(5);
RS485.write(lightLevel*100 /1000);
delayMicroseconds(5);
RS485.write(NUL);
delayMicroseconds(5);
RS485.write(NUL);
delayMicroseconds(5);
RS485.write(NUL);
delayMicroseconds(5);
RS485.write(NUL);
delayMicroseconds(5);
RS485.flush();
}
int bufferDelay = 10;
delayMicroseconds(bufferDelay);
digitalWrite(RS485inout, RS485Receive);
digitalWrite(ledPin, LOW);
delayMicroseconds(10);
//---------- I/O status // declarations ------------------------------------------------------------------
{
if (proxMotion == HIGH)
{
proxMotion_LED = 100;
}
if (proxMotion_LED == 100)
{
digitalWrite(9,HIGH);
}
else
{
proxMotion = 99;
proxMotion_LED = 99;
digitalWrite(9,LOW);
}
//------------------------------------------------//
if (inMotion == HIGH)
{
inMotion_LED = 100;
}
if (inMotion == 100)
{
digitalWrite(8,HIGH);
}
else
{
inMotion = 99;
}
//------------------------------------------------//
if (outMotion == HIGH)
{
outMotion_LED = 100;
}
if (outMotion == 100)
{
digitalWrite(7,HIGH);
}
else
{
outMotion = 99;
}
//------------------------------------------------//
if
(gasSensor_Alarm == LOW && gasSensor_Heartbeat == HIGH) {
gasSensor_Alarm_LED = HIGH;
gasSensor_Alarm = 100;
Alarm_Status = 100;
digitalWrite(6,HIGH);
}
else
{
gasSensor_Alarm = 99;
Alarm_Status = 99;
}
//------------------------------------------------//
if (gasSensor_Heartbeat == HIGH)
{
gasSensor_Heartbeat_LED = 1;
gasSensor_Heartbeat_Fault = 99;
}
if (gasSensor_Heartbeat_LED == 1)
{
digitalWrite(5,HIGH);
}
if (gasSensor_Heartbeat == LOW)
{
gasSensor_Heartbeat_LED = 0;
gasSensor_Heartbeat_Fault = 100;
}
if (gasSensor_Heartbeat_LED == 0)
{
digitalWrite(5,LOW);
}
//------------------------------------------------//
if (Alarm_Status == 100)
{
buzzerAlarm = 100;
digitalWrite(4,HIGH);
}
//--------- Luminosity Value -------------------------------------------------------------------
float LumenValue = analogRead(LIGHTSENSORPIN); //Read light level
float square_ratio = LumenValue / 1023.0; //Get percent of maximum value (1023)
square_ratio = pow(square_ratio, 2.0); //Square to make response more obvious
lightLevel = LumenValue ;
F_temperature = (bme.temperature*1.8+32);
unsigned long endTime = bme.beginReading();
if (!bme.endReading()) {
Serial.println(F("Failed to complete reading :("));
return;
}
Serial.print(F("Reading completed at "));
Serial.println(millis());
Serial.print(F("Temperature = "));
Serial.print(bme.temperature);
Serial.println(F(" *C"));
Serial.print(F("Pressure = "));
Serial.print(bme.pressure / 100.0);
Serial.println(F(" hPa"));
Serial.print(F("Humidity = "));
Serial.print(bme.humidity);
Serial.println(F(" %"));
Serial.print(F("Gas = "));
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(F(" KOhms"));
Serial.print(F("Approx. Altitude = "));
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(F(" m"));
Serial.println();
}
}
while((!RS485.available()) || (RS485.read() != 'u'))
{
// really do nothing
}
You are reading some of the input pins once in setup():
//---------------------------------------
pinMode(A0, INPUT);
proxMotion = digitalRead(A0);
//---------------------------------------
pinMode(A1, INPUT);
inMotion = digitalRead(A1);
//---------------------------------------
pinMode(A2, INPUT);
outMotion = digitalRead(A2);
//---------------------------------------
pinMode(3, INPUT);
gasSensor_Alarm = digitalRead(3);
//---------------------------------------
pinMode(2, INPUT);
gasSensor_Heartbeat = digitalRead(2);
Unless you read them again inside loop() you can't expect the variable to magically change if the input changes.