hey guys so i'm having an issue where i need the sensor on a live stock drafting cage to work with more certainty..
at the moment the sensor detect an animal and clamps the animal. a comand to weigh the animal is sent to a weigh scale unit.
a weight category is sent back...now this is where it gets tricky.. once the weight category is sent back the clamp releases along with a rear gate, and i have set the sensor up to only re activate once a timer has become true..the idea was that the animal should be out by then, then the sensor would be ready for the next animal to enter and be sensed.. however in real life it isnt working like that and i need to hold the sensor from retriggering if an animal is still in there and once it has left begining a very short timer of 1 second before it will re activate..
all help would be much appreciated!
#include <SPI.h>
const int PinOne = 1;
const int PinTwo = 2; //sensor
const int PinThree = 3;
const int PinFour = 4;
const int PinFive = 5;
const int PinSix = 6;
const int PinSeven = 7;
const int PinEight = 8;
const int PinNine = 9;
const int PinTen = 10;
const int PinEleven = 11;
const int PinTwelve = 12 ;// Pin Addresses
int incomingByte;
static bool PinTwoActive;
unsigned long interval = 2000;
unsigned long startTime;
byte lastPress;
byte lastPress1;
byte lastPress2;
byte lastPress3;
byte lastPress4;
// a variable to read incoming serial data into
void setup() {
// initialize serial communication:
Serial.begin(38400);
digitalWrite(PinNine,HIGH);
digitalWrite(PinTen,HIGH);
digitalWrite(PinEleven,HIGH);
digitalWrite(PinTwelve,HIGH);
PinTwoActive = true;
// initialize the pins:
// INPUTS
pinMode(PinTwo, INPUT);
pinMode(PinThree, INPUT);
pinMode(PinFour, INPUT);
pinMode(PinFive, INPUT);
pinMode(PinSix, INPUT);
pinMode(PinSeven, INPUT);
//OUTPUTS
pinMode(PinEight, OUTPUT );
pinMode(PinNine, OUTPUT );
pinMode(PinTen, OUTPUT);
pinMode(PinEleven, OUTPUT);
pinMode(PinTwelve, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
}
byte pressed1 = (PinTwoActive && digitalRead(PinTwo));
if (pressed1)
{
if (pressed1 != lastPress1)
{
Serial.print ("{RH}");
lastPress1 = pressed1;
}
}
{
lastPress1 = pressed1;
}
byte pressed2 = (PinTwoActive && digitalRead(PinTwo));
if (pressed2)
{
if (pressed2 != lastPress2)
{
digitalWrite(PinTen, LOW);
lastPress2 = pressed2;
}
}
lastPress2 = pressed2;
{
}
byte pressed3 = (PinTwoActive && digitalRead(PinTwo));
if (pressed3)
{
if (pressed3 != lastPress3)
{
digitalWrite(PinNine, LOW);
lastPress3 = pressed3;
}
}
lastPress3 = pressed3;
{
}
if (digitalRead(PinThree)) // Draft Left
digitalWrite(PinEleven, LOW);
{
}
if (digitalRead(PinThree)) // Draft Left
digitalWrite(PinTwelve, HIGH);
{
}
if (digitalRead(PinFour)) // Draft Centre
digitalWrite(PinEleven, HIGH);
{
}
if (digitalRead(PinFour)) // Draft Centre
digitalWrite(PinTwelve, HIGH);
{
}
if (digitalRead(PinFive)) // Draft Right
digitalWrite(PinTwelve, LOW);
{
}
if (digitalRead(PinFive)) // Draft Right
digitalWrite(PinEleven, HIGH);
{
}
if (digitalRead(PinSix))
digitalWrite(PinTen, HIGH);//CLAMP REMOTE RELEASE
{
}
if (digitalRead(PinSix))
digitalWrite(PinNine, HIGH); //GATE REMOTE RELEASE
{
}
if (incomingByte == '1')
digitalWrite(PinTen, HIGH); // RELEASE COMMAND
{
}
if (incomingByte == '2')
digitalWrite(PinTen, HIGH);
//^^^^
{
}
if (incomingByte == '3')
digitalWrite(PinTen, HIGH);
//^^^^
{
}
if (incomingByte == '1')
PinTwoActive = false; // RELEASE COMMAND
{
}
if (incomingByte == '2')
PinTwoActive = false;
//^^^^
{
}
if (incomingByte == '3')
PinTwoActive = false;
//^^^^
{
}
if (incomingByte == '1') // Draft Left
digitalWrite(PinEleven, LOW);
{
}
if (incomingByte == '1') // Draft Left
digitalWrite(PinTwelve, HIGH);
{
}
if (incomingByte == '2') // Draft Centre
digitalWrite(PinEleven, HIGH);
{
}
if (incomingByte == '2') // Draft Centre
digitalWrite(PinTwelve, HIGH);
{
}
if (incomingByte == '3') // Draft Right
digitalWrite(PinTwelve, LOW);
{
}
if (incomingByte == '3') // Draft Right
digitalWrite(PinEleven, HIGH);
{
}
if (incomingByte == '1')
digitalWrite(PinEight, HIGH);
{
}
if (incomingByte == '2')
digitalWrite(PinEight, HIGH);
{
}
if (incomingByte == '3')
digitalWrite(PinEight, HIGH);
{
}
byte pressed = digitalRead(PinSeven);
if (pressed)
{
if (pressed != lastPress)
{
startTime = millis();
}
}
lastPress = pressed;
if (digitalRead(PinSeven))
{
if (millis() - startTime >= interval)
{
digitalWrite(PinNine, HIGH);
digitalWrite(PinTen, HIGH);
digitalWrite(PinEight,LOW);
Serial.print("{RD}");
PinTwoActive = true; // this is where i need to have some advice.....i only want this to be active once the timer has completed and if the sensor is not triggered at the time...????
}
}
}