Heres a Challenging one for yas! :) PinActive code issues...

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…???

}

}
}

You could always add a sensor on the rear gate which detects when that one is closed - once it has been opened after weighing, wait for it to close before resetting for the next animal. (Presumably you close that gate before letting the next one in).

Alternatively, wait for the sensor that detects the animal to show it isn't there anymore, (then maybe wait a bit just in case), then start checking for the next animal.

I would like to help but because you have not put your code in code tags and you have literally dozens of unnecessary blank lines the code is difficult to read and understand. Use code tags and format it better and I will take a look.

One thing that I did notice early on in the code was that you have given totally meaningless names to pins such as const int PinOne = 1;Why ?

im no expert and am self taught so i appologise for the amature style of my code...im really trying my best here!

NZarduino:
im no expert and am self taught so i appologise for the amature style of my code…im really trying my best here!

But surely you can see that the layout is crap, particularly the blank lines by the dozen, and you will, of course, have read http://forum.arduino.cc/index.php?topic=97455.0 before posting your programming question.

it makes it a lot easier for me to read which section is which if i add space between...i didn't think it was such an issue?

cheers for the hate dude

“Code tags” are where you type

</mark> <mark>[code]</mark> <mark>

on one line
followed by your code/sketch
and the

</mark> <mark>[/code]</mark> <mark>

on the line after all that.
That makes the window with the slider, you may have noticed.
It’s easier on the eyes and keeps the presentation better organized.

I use “devices” like
// ------------------------
for example
to section off routines and so on.

Carry on.

Adopt, Adapt, Be Resilient

OK For the benefit of anyone that may want to debug your code, I’ve taken out all the spaces. Personally I’d rather have all the details of what every pin is connected to and do a complete rewrite.

#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...????

    }
  }
}

Hi, after you have weighted the animal, does the weighing device still measure the weight if the animal has not moved off.

You could get the program to wait until the weight measurement is below a certain amount, signifying that the animal has moved on.

Tom… :slight_smile:

It is a challenge… to make sense of it all.

You should set the pinMode before you set their state:

void setup() 
{
  // initialize serial communication:
  Serial.begin(38400); 
  digitalWrite(PinNine,HIGH);//<<<<<<<<<<<you are telling PinNine to go 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 );//<<<<<<<< before you told your arduino that you were going to use it as an OUTPUT
  pinMode(PinTen, OUTPUT);
  pinMode(PinEleven, OUTPUT);
  pinMode(PinTwelve, OUTPUT);
}

you know that an if statement can accommodate more than one action…?

if (incomingByte == '1')
    digitalWrite(PinTen, HIGH); // RELEASE COMMAND/
    //
  {
    //digitalWrite(PinTen, HIGH); // RELEASE COMMAND/<<<<<<<<<<<<you can put that here<<<<
    //PinTwoActive = false; // RELEASE COMMAND<<<<<<<<<<<<<<<<the other one below... here
  }   
  if (incomingByte == '2')
    digitalWrite(PinTen, HIGH); 
  //^^^^
  { 
  } 
  if (incomingByte == '3')
    digitalWrite(PinTen, HIGH);
  //^^^^
  {    
  }
  if (incomingByte == '1')
    PinTwoActive = false; // RELEASE COMMAND<<<<<<<<<<<<<<<<the other one
  {
  }   
  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);
  {

or, is it that you really don’t yet understand how to handle your serial input here:

  if (Serial.available() > 0) 
  {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
  }

i am using one of those RElAY cards that are Activated when they get a signal LOW from the arduino, to counter it i changed my highs and lows and set the pins for relay output to be high when the system starts to prevent the relays becoming active untill the loop starts.

But could you give us a list, pin by pin, of what all of your inputs and outputs do?

eg Pin1 Opens Door Pin2 Activates clamp pin 3 Sends message to weighing machine pin 5 Microswitch to front door (high when open) etc...

Pin0 = bluetooth rx hc 05 unit pin1 = bluetooth tx hc 05 unit pin2 = Sensor..on/off pin3 = handheld remote control, left gate select pin4 = handheld remote control, centre gate select pin5 = handheld remote control right gate select pin6 = handheld remote control clamp and gate release pin7 & pin 8 are used as a physical logic relay pin9 output relay left gate pin10 output relay right gate pin11 output clamp .... clamps animal pin12 output Rear gate....stops more animals from entering pen

have you read through this yet?

yes reading currently.. i dont have the time to do a full rewrite..the code i have is working currently its just one thing i want to add to it, does anyone see it being possible given my current code??

NZarduino: it makes it a lot easier for me to read which section is which if i add space between...i didn't think it was such an issue?

cheers for the hate dude

No hate here, just an intense dislike. I agree with your reasons for separating code sections with blank lines, I do it myself, but this is the sort of thing that I was talking about

  {
    lastPress1 = pressed1;
  }















  byte pressed2 = (PinTwoActive && digitalRead(PinTwo));
  if (pressed2)

15 blank lines .......

NZarduino: cheers for the hate dude

  }














  }
}

Lol. Get a grip, man. This is ridiculous.

How to use this forum

Code tags.