Hello,
I'm having an issue with a portion of my program, I think I'm getting it stuck in an if statement but I can't figure out how to get it out. This is what the program is supposed to do:
There are parts being placed on a conveyor belt, and the conveyor is moving at a constant speed. Once a part reaches a certain point on the belt, a sensor will detect the part and send a LOW signal. This will tell the conveyor to stop, and a camera will take a picture of the part. If the reading is BAD, a solenoid will be activated and the part will be blown off of the conveyor, then the conveyor will start back up and that process will continue. If the reading is GOOD, no action is needed and the conveyor will simply start back up again and the process will continue.
The program works as it should for a BAD reading, but it is getting stuck on the GOOD reading because it contradicts the previous if statement that tells it to stop if a part is detected. I feel like I've tried every variation of this I can think of but nothing seems to work and I seem to just cause other areas of the code to stop working whenever I try to change something. Any ideas here? Should I be using something other than an if statement? Coding is new to me so any help is appreciated
#include <AccelStepper.h>
#define MotorInterfaceType 1
#define dirPin 6
#define stepPin 3
AccelStepper stepper = AccelStepper(MotorInterfaceType, stepPin, dirPin);
int NGsolenoid = 13;
int OKpin = 10;
int NGpin = 11;
int GOOD;
int BAD;
int trigger = 8;
int IRsensor = 0;
int IRsensorcall;
int i;
void setup() {
pinMode(NGsolenoid, OUTPUT);
pinMode(OKpin, INPUT);
pinMode(NGpin, INPUT);
pinMode(trigger, OUTPUT);
pinMode(IRsensor, INPUT);
stepper.setMaxSpeed(800);
stepper.setSpeed(800);
GOOD = digitalRead(OKpin);
BAD = digitalRead(NGpin);
Serial.begin(9600);
}
void loop() {
i=0;
stepper.runSpeed();
IRsensorcall = digitalRead(IRsensor);
digitalWrite(trigger, LOW);
if (IRsensorcall == LOW && i == 0){ //if part is present, stop the motor and trigger the camera
stepper.setSpeed(0);
stepper.runSpeed();
delay(200);
digitalWrite(trigger,HIGH);
delay(500);
}
if (BAD == HIGH && IRsensorcall == LOW && GOOD == LOW){ //if the reading from the camera is BAD, turn on the solenoid to blow off the part, then turn the motor back on and go back to the start of the loop
digitalWrite(NGsolenoid, HIGH);
delay(500);
digitalWrite(NGsolenoid, LOW);
stepper.setSpeed(800);
stepper.runSpeed();
}
else { //if the reading from the camera is good,turn the motor back on and go back to the start of the loop
i=1;
stepper.setSpeed(800);
stepper.runSpeed();
}
}
So my thought is that the issue is that the movement of the conveyor (whether it is on or off) is determined by the presence sensor reading. The part is removed with a bad reading but it stays present with a good reading, so I want the conveyor to start again but it won't because it's been told not to move if the part is present (but I need it to stop when a part is present for the camera to get a picture). Basically I thought adding some sort of second criteria would help it escape that loop, so I had it set i=0 at the beginning and make the i=0 a second criteria for the if statement, then set i=1 if the part has a good reading so that it will fail that second criteria and start moving again. I have to have it reset back to i=0 because it needs to stop and take a picture every time the sensor encounters a part. But unfortunately this isn't working
Your program is not logic, it should be something like this:
void loop()
{
static bool partDetected = false;
if (partDetected == false && digitalRead(IRsensor) == LOW) //if part is present, stop the motor and trigger the camera
{
partDetected = true;
stepper.setSpeed(0);
stepper.runSpeed();
delay(200);
digitalWrite(trigger,HIGH);
delay(500); // wait camera result
digitalWrite(trigger, LOW);
if (digitalRead(NGpin) == HIGH) //if the reading from the camera is BAD, turn on the solenoid to blow off the part, then turn the motor back on and go back to the start of the loop
{
digitalWrite(NGsolenoid, HIGH);
delay(500);
digitalWrite(NGsolenoid, LOW);
}
stepper.setSpeed(800);
stepper.runSpeed();
}
else
{
partDetected = false;
}
}
Not saying it will work, but more likely to work...
sbbs20:
So my thought is that the issue is that the movement of the conveyor (whether it is on or off) is determined by the presence sensor reading. The part is removed with a bad reading but it stays present with a good reading, so I want the conveyor to start again but it won't because it's been told not to move if the part is present (but I need it to stop when a part is present for the camera to get a picture).
What @guix has posted will get you 99% there. What's needed now is state change detection. That is, not that the part *is present * but that it has just become present.
bool isPartPresent = digitalRead(IRsensor);
static bool wasPartPresentBefore;
if (isPartPresent == LOW and wasPartPresentBefore == HIGH) //if part arrived, stop the motor and trigger the camera
{
// if statement...
}
wasPartPresentBefore = isPartPresent;