Arduino Code Help

Here is the code with my modification in bold.

/*
Feeder
using Bounce2 library to get stable sensor states
see https://github.com/thomasfredericks/Bounce2/wiki
*/

#include <Bounce2.h>

#define ASENSOR 7 //alarm IR sensor is on pin 7
#define ALED 8 //alarm LED is on pin 8
#define ABUZZ 9 //alarm buzzer relay is on pin 9
#define RELAY1 10 //feeder1 relay is on pin 10
#define RELAY2 11 //feeder2 relay is on pin 11
#define SENSOR1LOW 2 //feeder1 Low IR Sensor is on pin 2
#define SENSOR1HIGH 3 //feeder1 High IR Sensor is on pin 3
#define SENSOR2LOW 4 //feeder2 Low IR Sensor is on pin 4
#define SENSOR2HIGH 5 //feeder2 High IR Sensor is on pin 5

// state variable
int asensorState = 0;
bool motor1On = false;
bool motor2On = false;

// edits for Bounce - detecting stable signal
#define SENSORDELAY 50 // milliseconds that the sensor must be stable to change reading
Bounce sensor1high = Bounce();
Bounce sensor2high = Bounce();
Bounce sensor1low = Bounce();
Bounce sensor2low = Bounce();

void setup() {
pinMode(ALED, OUTPUT); // initialize the ALED pin as an output:
pinMode(ABUZZ, OUTPUT); // initialize the ABUZZ pin as an output:
pinMode(ASENSOR, INPUT); // initialize the ASENSOR pin as an input:
pinMode(RELAY1, OUTPUT); // initialize the RELAY1 pin as an output:
pinMode(RELAY2, OUTPUT); // initialize the RELAY2 pin as an output:
pinMode(SENSOR1LOW, INPUT_PULLUP); // sensor1 Low with pullup enabled
pinMode(SENSOR1HIGH, INPUT_PULLUP); // sensor1 High with pullup enabled
pinMode(SENSOR2LOW, INPUT_PULLUP); // sensor2 Low with pullup enabled
pinMode(SENSOR2HIGH, INPUT_PULLUP); // sensor2 High with pullup enabled

digitalWrite(ASENSOR, HIGH); //turn on the pullup
digitalWrite(RELAY1, HIGH); // make sure the relay is Off after reset
digitalWrite(RELAY2, HIGH); // make sure the relay is Off after reset

// edits for Bounce
sensor1high.attach(SENSOR1HIGH);
sensor1high.interval(SENSORDELAY);
sensor2high.attach(SENSOR2HIGH);
sensor2high.interval(SENSORDELAY);
sensor1low.attach(SENSOR1LOW);
sensor1low.interval(SENSORDELAY);
sensor2low.attach(SENSOR2LOW);
sensor2low.interval(SENSORDELAY);
}

void loop() {
sensor1low.update(); // update sensor1low instance:
sensor2low.update(); // update sensor12low instance:

int value1 = sensor1low.read(); //get updtaed value:
int value2 = sensor2low.read(); //get updtaed value:

if ( value1 == HIGH ) {
digitalWrite(SENSOR1LOW, LOW );
}
else {
digitalWrite(SENSOR1LOW, HIGH );
}
if ( value2 == HIGH ) {
digitalWrite(SENSOR2LOW, LOW );
}
else {
digitalWrite(SENSOR2LOW, HIGH );
}


sensorsUpdate();
if (motor1On == false) { // motor is stopped
if (sensor1low.rose()) { // low sensor empty
startFeeder1();
}
}
else { // motor is running
if (sensor1high.fell()) { // high sensor full
stopFeeder1();
}
}
if (motor2On == false) { // motor is stopped
if (sensor2low.rose()) { // low sensor empty
startFeeder2();
}
}
else { // motor is running
if (sensor2high.fell()) { // high sensor full
stopFeeder2();
}
}

checkASensor();
}

void sensorsUpdate(){
sensor1high.update();
sensor1low.update();
sensor2high.update();
sensor2low.update();
}
void stopFeeder1(){
digitalWrite(RELAY1, HIGH); // turn motor off
motor1On = false; // record motor state
}
void stopFeeder2(){
digitalWrite(RELAY2, HIGH); // turn motor off
motor2On = false; // record motor state
}

void startFeeder1(){
digitalWrite(RELAY1, LOW); // turn motor on
motor1On = true; // record motor state
}
void startFeeder2(){
digitalWrite(RELAY2, LOW); // turn motor on
motor2On = true; // record motor state
}

void checkASensor(){
asensorState = digitalRead(ASENSOR); // read the state of the ASENSOR value:

if (asensorState == LOW) { // check if the sensor beam is broken, if it is, the sensorState is LOW:
digitalWrite(ALED, LOW); // turn LED on:
digitalWrite(ABUZZ, LOW); // turn BUZZER on:
}
else {
digitalWrite(ALED, HIGH); // turn LED off:
digitalWrite(ABUZZ, HIGH); // turn BUZZER off:
}
}
 
Back
Top