some issues with program not running correctly

Hi everyone, i am having some issues with a program and it is driving me crazy, the problem is difficult to describe. i seem to be getting very random results. i have a small conveyor and a couple of servo's that feed material down a slide until the bottom, at which point the Laser beam is broken and the LDR registers a lower reading. it seems really simple however sometimes it works perfectly sometimes then on the next cycle it doesn't. sometimes it doesn't even wait for the beam to be broken before it sends down another product. i know this sounds like a circuit problem however i am pretty sure i have it correct. has anyone ever experienced any issues like this? i can post the code if that is helpful?

Start here

It's also exceedingly likely that your input from the beam at the bottom is getting corrupted. How long are the cables? A schematic of your whole setup would be useful.

The image attached is the circuit.
The wires are all about 1m long or shorter.
the code is below
the only difference between this and what i am using is that each of the functions are on different tabs

Code:

#include <Servo.h>

int ProductEdjectIO = 6;
int SideServoIO = 5;

int ProductConveyor = 11;
int LASER = 10;
int LDR = A0;

Servo ProductEdject;
Servo SideServo;

int Cycles = 1;
int Sensor = 0;

void setup()
{  
 ProductEdject.attach(ProductEdjectIO);
 SideServo.attach(SideServoIO);
 
 SideServo.write(90);
 ProductEdject.write(90);
 
 pinMode(ProductConveyor,OUTPUT);
 pinMode(LASER,OUTPUT);
 pinMode(A0,INPUT);
 Serial.begin(9600);


}

void loop(){

 Product1();
 delay(500);
 Product2();
 delay(500);
 Product3();
 delay(500);
 Product4();
 delay(500);
 
}

void GetSensor(){
int Val1 = 0;
int Val2 = 0;
int Val3 = 0;
int Val4 = 0;
int Val5 = 0;
int Val6 = 0;
int Val7 = 0;
int Val8 = 0;
int Val9 = 0;
int Val10 = 0;

Val1 = analogRead(A0);
delay(5);
Val2 = analogRead(A0);
delay(5);
Val3 = analogRead(A0);
delay(5);
Val4 = analogRead(A0);
delay(5);
Val5 = analogRead(A0);
delay(5);
Val6 = analogRead(A0);
delay(5);
Val7 = analogRead(A0);
delay(5);
Val8 = analogRead(A0);
delay(5);
Val9 = analogRead(A0);
delay(5);
Val10 = analogRead(A0);
delay(5);

int total = Val1+Val2+Val3+Val4+Val5+Val6+Val7+Val8+Val9+Val10;
Sensor = total/10;
}

void Product1(){
 
 digitalWrite(ProductConveyor,HIGH);
 delay(100);
 digitalWrite(LASER,HIGH);
 delay(100);
 SideServo.write(70);
 delay(100);
   
     ProductEdject.write(0);
     delay(1000);
     ProductEdject.write(180);
     delay(1000);
     ProductEdject.write(90);
     delay(500);
     
     GetSensor();
     while(Sensor >=800){
         digitalWrite(ProductConveyor,HIGH);
         delay(50);
         GetSensor();
      
     }
     digitalWrite(ProductConveyor,LOW);
     delay(500);
   
   
     GetSensor();
     while(Sensor <=600){
         digitalWrite(ProductConveyor,LOW);
         digitalWrite(LASER,LOW);
         delay(500);
         digitalWrite(LASER,HIGH);
         delay(50);
         GetSensor();
     }
}

void Product2(){
  digitalWrite(LASER,HIGH);
  delay(100);
  SideServo.write(40);
  delay(100);
  digitalWrite(ProductConveyor,HIGH);
  delay(100);

     Sensor = analogRead(A0);
     while(Sensor >=800){
         digitalWrite(ProductConveyor,HIGH);
         delay(50);
         Sensor = analogRead(A0);

     }
     digitalWrite(ProductConveyor,LOW);
     delay(500);
   
     Sensor = analogRead(A0);
     while(Sensor <=600){
         digitalWrite(ProductConveyor,LOW);
         delay(100);
         digitalWrite(LASER,LOW);
         delay(500);
         digitalWrite(LASER,HIGH);
         delay(50);
         Sensor = analogRead(A0);
     }
}


void Product3(){
  digitalWrite(LASER,HIGH);
  delay(100);
  SideServo.write(28);
  delay(100);
  digitalWrite(ProductConveyor,HIGH);
  delay(100);

     Sensor = analogRead(A0);
     while(Sensor >=800){
         digitalWrite(ProductConveyor,HIGH);
         delay(50);
         Sensor = analogRead(A0);
  
     }
     
     digitalWrite(ProductConveyor,LOW);
     delay(500);
    
     Sensor = analogRead(A0);
     while(Sensor <=600){
        digitalWrite(ProductConveyor,LOW);
         digitalWrite(LASER,LOW);
         delay(500);
         digitalWrite(LASER,HIGH);
         delay(50);
         Sensor = analogRead(A0);
     }
}

void Product4(){
  digitalWrite(LASER,HIGH);
  delay(100);
  SideServo.write(16);
  delay(100);
  digitalWrite(ProductConveyor,HIGH);
  delay(100);

     Sensor = analogRead(A0);
     while(Sensor >=800){
         digitalWrite(ProductConveyor,HIGH);
         delay(50);
         Sensor = analogRead(A0);

     }
     
     
     digitalWrite(ProductConveyor,LOW);
     delay(500);
   
     Sensor = analogRead(A0);
     while(Sensor <=600){
         digitalWrite(ProductConveyor,LOW);
         digitalWrite(LASER,LOW);
         delay(500);
         digitalWrite(LASER,HIGH);
         delay(50);
         Sensor = analogRead(A0);
     }
}

Remove ALL the delay()s from your code and use millis() to manage timing as illustrated in several things at a time.

Also make a pencil drawing of the circuit and post a photo of that. It will be much more informative than a photo of the hardware.

...R

Hi, Robin2, got you well trained.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom.... :)

I hope this is what you are after

LDR's are very slow. It might not recover in time for the next item. Serial.print() the values after the beam is broken to see how long it takes to get back up.

thanks everyone for your quick replies. i have managed to fix the issue, i added a second power supply and connected it to VCC and GND. i guess this means that the power was dropping on the arduino and causing the program to reset or something. very frustrating.? does anyone have a good solution for a stable single power source circuit? i am thinking i should add a capacitor and diode for the VCC pin? any thoughts?

Hi,

thanks everyone for your quick replies. i have managed to fix the issue, i added a second power supply and connected it to VCC and GND. i guess this means that the power was dropping on the arduino and causing the program to reset or something.

Which is what a full complete circuit diagram would have shown. (power supply)

Tom..... :) Good to hear you got it going.