Need a code check to see if this should work.

*/

Created by Stefan Cramer
February 23, 2010
Crown Audio
A division of Harman International

//What I am trying to accomplish:

//I am running a production line on which there is a reflow oven with a fairly non intelligent conveyor system.

//What I hope to successfully create is a control that will stop the input conveyor and sound an audible alarm if too many boards ent
r the reflow oven.

//The idea is to have:

//1) An entrance sensor increment a “boards in oven” variable.
//2) An exit sensor decrement the “boards in oven” variable.
//3) Monitor the “boards in oven variable”
//4) If the “boards in oven” variable goes above a “max load” variable then the conveyor will be temporarily be shut off and an audible
sound will alarm.
//5) After an alarm is sounded and all boards have been cleared from the oven the alarm will then turn off and the conveyor will rest
rt.

//Please take a look at what I have so far and let me know if I am on the right track or if I need to start from scratch.

//Also I am needing to know if this application fits the DEV-00666 (Arduino Duemilanove)

//Thanks,
//Stefan Cramer

*/

int ovenentry = 0; // Oven entry point sensor.
int ovenexit = 1; // Oven exit point sensor.
int enteroven = 0; // variable to store the value from ovenentry sensor.
int exitoven = 0; // variable to store the value from ovenexit sensor.
int inoven = 0; // variable to store live oven quantity.
int safeload = 0; // Empty machine allowing production to again continue safely.
int maxload = 9; // Maximum amount of boards allowed in machine at once.
int alarm = 3; // Piezo alarm trigger.
int conveyor = 9; // Conveyor shutoff trigger.

void setup() {
// declare the alarm as an OUTPUT;
pinMode(alarm, OUTPUT);
// declare the alarm as an OUTPUT;
pinMode(conveyor, OUTPUT);
}

void loop() {
// read the value from oven entry sensor.
enteroven = analogRead(ovenentry);
// read the value from oven exit sensor.
exitoven = analogRead(ovenexit);

// Add 1 board to inoven variable if enteroven sensor reads over 4 volts.
if (enteroven > 4)
// Add one board to inoven variable.
inoven = inoven + 1;

// Subtract 1 board to inoven variable if exitoven sensor reads over 4 volts.
if (exitoven > 4)
// Subtract one board to inoven variable.
inoven = inoven - 1;

// Check to see if more boards are in oven then allowed.
if (inoven > maxload)
// turn the alarm on.
digitalWrite(alarm,HIGH);
// turn off the input conveyor.
digitalWrite(conveyor,HIGH);

if (alarm == HIGH)(inoven == safeload);
// turn the alarm off.
digitalWrite(alarm,LOW);
// turn on the input conveyor.
digitalWrite(conveyor,LOW);
}

Well, you have a decent start but there are a few problems. AnalogRead returns a value between 0 and 1023. http://arduino.cc/en/Reference/AnalogRead Depending on your sensor, you may actually be better off using a digital pin with HIGH and LOW values. Another issue is that you never wait for the sensor to go off so your count will almost instantly be hit as the code loops.

Do you have any more information about your conveyor and other line equipment? Most conveyors have a SMEMA interface that provides the control and sensing you require. If you are controlling the conveyor I don’t see a real need for the alarm as you shouldn’t ever allow more than the maximum number of boards into the conveyor. It’s hard to say what your best option is without know more about exactly what your upstream and downstream equipment is and how it operates.

Thanks for the the reply Digger,

The conveyors are CR technology, and the oven is a heller. While they do have limited functioning SMEMA. As it stands it does not let boards enter if a board is on the exit sensor. What we are interested in however is a flag for our operators to let them know if more boards are in the oven then we want. In addition I added an output to stop the conveyor as well to keep any more boards from entering the oven until the issue has been dealt with.

I made changes to to look for change state high to low which should take care of the counting too quickly issue. I also made changes to use a digital input rather then the analog.

How does it look now? Thanks again for your help.

/*
Created by: Stefan Cramer
Company: Crown Audio

The point of this sketch is to have:

  1. An entrance sensor increment a “boards in oven” variable.
  2. An exit sensor decrement the “boards in oven” variable.
  3. Monitor the “boards in oven variable”
  4. If the “boards in oven” variable goes above a “max limit”
    variable then the conveyor will be temporarily be shut off
    and an audible sound will alarm
  5. After an alarm is sounded and all boards have been cleared
    from the oven the alarm will then turn off and the conveyor
    will restart

*/

// these constants don’t change:
const int entrysensor = 1; // the pin that the entry sensor is attached to
const int exitsensor = 2; // the pin that the exit sensor is attached to
const int alarm = 3; // the pin that will trigger audible alarm
const int conveyor = 9; // the pin that will trigger pause in the conveyor
const int maxlimit = 9; // the maximum allowed number of boards in the machine at a time

// These variables will change:
int boardqty = 0; // counter for the number of boards inside the oven
int entrystate = 0; // current state of entry sensor
int lastentrystate = 0; // previous state of entry sensor
int exitstate =0; // current state of exit sensor
int lastexitstate =0; // previous state of exit sensor

void setup() {
// initialize the entry sensor as a input:
pinMode(entrysensor, INPUT);
// initialize the exit sensor as a input:
pinMode(exitsensor, INPUT);
// initialize the alarm pin as a output:
pinMode(alarm, OUTPUT);
// initialize the conveyor pin as a output:
pinMode(conveyor, OUTPUT);

}

void loop() {
// read the entry and exit sensor input pins:
entrystate = digitalRead(entrysensor);
exitstate = digitalRead(exitsensor);

// This section is for incrementing the board qty
// if a state change is seen from high to low on
// the entrance sensor

// compare entrystate to lastentrystate
if (entrystate != lastentrystate) {
// when the state has changed see if change went
// HIGH to LOW
if (entrysensor == HIGH) {
// Board entered machine, increment board quantity
boardqty++;
}
else {
// do nothing
}
}

// save the current entrystate as the lastentrystate,
// for next time through the loop
lastentrystate = entrystate;

// This section is for decrementing the board qty
// if a state change is seen from high to low on
// the exit sensor

// compare exitstate to lastexitstate
if (exitstate != lastexitstate) {
// when the state has changed see if change went
// HIGH to LOW
if (exitsensor == HIGH) {
// Board exiteded machine, decrement board quantity
boardqty–;
}
else {
// do nothing
}
}

// save the current exitstate as the lastexitstate,
// for next time through the loop
lastexitstate = exitstate;

// This section is for comparing the board qty to the
// max board qty and seeing if the alarm and conveyor
// stoppage should be triggered

// compare maxlimit to boardqty
if (boardqty >= maxlimit) {
// When boardqty is >= the max limit trigger alarm
// and the conveyor
digitalWrite(alarm,HIGH);
digitalWrite(conveyor,HIGH);
}
else {
// When boardqty is < the max limit turn off alarm
// and restart the conveyor
digitalWrite(alarm,LOW);
digitalWrite(conveyor,LOW);
}
}

Looks like it should work. Maybe you are saving the else for future use, but it is not required. The only other thing is that the last state really only needs to be updated when it changes, but I think it will work fine as you have it. i.e.

if (entrystate != lastentrystate) {
  // save the current entrystate as the lastentrystate,
  // for next time through the loop
  lastentrystate = entrystate;

  // when the state has changed see if change went
  // HIGH to LOW
  if (entrysensor == HIGH) {
    // Board entered machine, increment board quantity
    boardqty++;
  } 
}

I've done some similar projects that involved a board stacker. Our main issue was that the ovens are unrelenting and keep pushing boards out even if the downstream is not ready.

Let us know how it goes!