Sensor Trouble

I'm trying to write a code for a board that has 2 photosensors on it. This test checks the pin heights of some parts. If the part is rejected it needs to lock out the LCD screen until the part is dropped down a "shoot". The part going by the photosensor in the shoot should unlock the LCD screen. I'm having a lot of trouble locking out the screen until that sensor responds to the board for some reason. Any thoughts?? This is the part that I need to lock out the LCD screen on. My full code is at the end.

void NGscreen (){ lcd.clear(); ngcount++; ngcount1=(int) ngcount; math1 = ngcount / totalcount;

math2 = math1 - 1; math2=abs(math2); yield = math2 * 100;

delay(50);

lcd.setCursor ( 0, 0 ); // go to the next line lcd.print("Total:"); lcd.setCursor ( 8, 0 ); // go to the next line lcd.print(totalcount1);

lcd.setCursor ( 12, 0 ); // go to the next line lcd.print("NG:"); lcd.setCursor ( 16,0 ); // go to the next line lcd.print(ngcount1);

lcd.setCursor ( 0, 1 ); // go to the next line lcd.print("Yield:"); lcd.setCursor ( 6,1 ); // go to the next line lcd.print(yield);

lcd.setCursor ( 12,1 ); // go to the next line lcd.print("%");

lcd.setCursor ( 0, 2 ); // go to the next line lcd.print(""); lcd.setCursor ( 7, 3 ); // go to the next line lcd.print("FAIL");

if (pin1==HIGH){ lcd.setCursor ( 18,2 ); // go to the next line lcd.print("5"); }

if (pin2==HIGH){ lcd.setCursor ( 19,2 ); // go to the next line lcd.print("4"); } if (pin3==HIGH){ lcd.setCursor ( 19,3 ); // go to the next line lcd.print("1"); } if (pin4==HIGH){ lcd.setCursor ( 18,3 ); // go to the next line lcd.print("2"); }

if (pin5==HIGH){ lcd.setCursor ( 17,3 ); // go to the next line lcd.print("3"); }

if (pin6==HIGH){ lcd.setCursor ( 17,2 ); // go to the next line lcd.print("6"); }

lcd.createChar(0, customChar); lcd.setCursor(16, 2); lcd.write(byte(0)); lcd.createChar(0, customChar); lcd.setCursor(16, 3); lcd.write(byte(0)); lcd.createChar(1, customChar1); lcd.setCursor(17, 1); lcd.write(byte(1)); lcd.createChar(1, customChar1); lcd.setCursor(18, 1); lcd.write(byte(1)); lcd.createChar(1, customChar1); lcd.setCursor(19, 1); lcd.write(byte(1)); lcd.createChar(3, customChar2); lcd.setCursor(16, 1); lcd.write(byte(3));

analogWrite(redledPin, 10); // turn LED ON analogWrite(greenledPin, 0); // turn LED OFF delay(2000);

lcd.clear(); lcd.setCursor ( 0 , 0 ); lcd.print ("REJECT"); lcd.setCursor ( 1 , 1 ); lcd.print ("DROP DOWN SHOOT"); delay (5000); }

This is first half of my code:

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x3F // Define I2C Address where the LCD is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

int n = 1;

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

byte customChar[8] = {
0b00111,
0b00111,
0b00111,
0b00111,
0b00111,
0b00111,
0b00111,
0b00111
};

byte customChar1[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111,
0b11111
};

byte customChar2[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00111,
0b00111,
0b00111
};

int redledPin = 3; // choose the pin for the red LED
int greenledPin = 4; // choose the pin for the green LED

int longyellow = 50; // choose the input pin (Micro Switch pin)
int red = 48; // choose the input pin (pogo pin)
int purple = 46; // choose the input pin (pogo pin)
int green = 44; // choose the input pin (pogo pin)
int orange = 42; // choose the input pin (pogo pin)
int blue = 40; // choose the input pin (pogo pin)
int yellow = 38; // choose the input pin (pogo pin)
int redblack = 36; // choose the input pin (pogo pin)
int purplewhite = 34; // choose the input pin (pogo pin)
int greenblack = 32; // choose the input pin (pogo pin)
int orangeblack = 30; // choose the input pin (pogo pin)
int blueblack = 28; // choose the input pin (pogo pin)
int whiteyellow = 26; // choose the input pin (pogo pin)

int resetbutton = 37; // choose the input pin (reset button)
int resetcount = 0; // variable for reset button (multiple pushes needed for reset- Poka-Yoke)
int resetbuttonstate = 1;
int resetlastbuttonstate= 1;

int programselect = A8; // choose the input pin (program select sensor)

int val = 0; // variable for reading the pin status

int trigger=1;
float totalcount = 0;
float ngcount = 0;
int buttonstate = 1;
int lastbuttonstate= 1;
float yield= 0 ;
float math1 = 0;
float math2 = 0;
float math3 = 0;
int totalcount1=0;
int ngcount1=0;
int lock = 0;
int pin1=0;
int pin2=0;
int pin3=0;
int pin4=0;
int pin5=0;
int pin6=0;

int keyencesensor = A8; //keyence sensor
int microtrigger1 = 31; //Rear center microswitch
int microtrigger2 = 33; //front left microswitch
int shootsensor = A9;

void setup()
{
lcd.begin (20,4); // start communication with lcd

pinMode(redledPin, OUTPUT); // declare LED as output
pinMode(greenledPin, OUTPUT); // declare LED as output

pinMode(longyellow, INPUT); // declare microswitch as input
digitalWrite(longyellow,HIGH); // turn internal pull-up resistor on

pinMode(red, INPUT); // declare pogo as input
digitalWrite(red,HIGH); // turn internal pull-up resistor on

pinMode(purple, INPUT); // declare pogo as input
digitalWrite(purple,HIGH); // turn internal pull-up resistor on

pinMode(green, INPUT); // declare pogo as input
digitalWrite(green,HIGH); // turn internal pull-up resistor on

pinMode(orange, INPUT); // declare pogo as input
digitalWrite(orange,HIGH); // turn internal pull-up resistor on

pinMode(blue, INPUT); // declare pogo as input
digitalWrite(blue,HIGH); // turn internal pull-up resistor on

pinMode(yellow, INPUT); // declare pogo as input
digitalWrite(yellow,HIGH); // turn internal pull-up resistor on

pinMode(redblack, INPUT); // declare pogo as input
digitalWrite(redblack,HIGH); // turn internal pull-up resistor on

pinMode(purplewhite, INPUT); // declare pogo as input
digitalWrite(purplewhite,HIGH); // turn internal pull-up resistor on

pinMode(greenblack, INPUT); // declare pogo as input
digitalWrite(greenblack,HIGH); // turn internal pull-up resistor on

pinMode(orangeblack, INPUT); // declare pogo as input
digitalWrite(orangeblack,HIGH); // turn internal pull-up resistor on

pinMode(blueblack, INPUT); // declare pogo as input
digitalWrite(blueblack,HIGH); // turn internal pull-up resistor on

pinMode(whiteyellow, INPUT); // declare pogo as input
digitalWrite(whiteyellow,HIGH); // turn internal pull-up resistor on

pinMode(keyencesensor, INPUT); // declare pogo as input
digitalWrite(keyencesensor,HIGH); // turn internal pull-up resistor on

pinMode(microtrigger1, INPUT); // declare pogo as input
digitalWrite(microtrigger1,HIGH); // turn internal pull-up resistor on

pinMode(microtrigger2, INPUT); // declare pogo as input
digitalWrite(microtrigger2,HIGH); // turn internal pull-up resistor on

pinMode(resetbutton, INPUT); // declare reset button as input
digitalWrite(resetbutton,HIGH); // turn internal pull-up resistor on

pinMode(programselect, INPUT); // declare reset button as input
digitalWrite(programselect,HIGH); // turn internal pull-up resistor on

Serial.begin(9600); // begin serial communication

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.clear();
lcd.setCursor ( 5, 0 );
lcd.print(“Interplex”);
lcd.setCursor ( 2, 1 ); // go to the next line
lcd.print(“Cooper Pin Tester”);
lcd.setCursor ( 0, 2 ); // go to the next line
lcd.print("");
lcd.setCursor ( 7, 3 ); // go to the next line
lcd.print(“AM-017”);

}

void loop()
{

while(lock < 1600){
buttonstate = digitalRead(longyellow);
trigger= digitalRead(longyellow);
RESETcounts ();
if (buttonstate != lastbuttonstate){

if (trigger==LOW){

Serial.print ("Program: ");
Serial.println (digitalRead(programselect));

if (digitalRead(programselect)==HIGH) {
READpins6();
VALcheck();
}else {
READpins5();
VALcheck();
}

}else {
READYscreen();
}

math1 = ngcount / totalcount;
math2 = math1 - 1;
math2=abs(math2);
yield = math2 * 100;
lastbuttonstate=buttonstate;
}

}

LockOutScreen (redledPin,greenledPin); // LockOutScreen function

}

// Functions BELOW

void LockOutScreen (int RedLED, int GreenLED){

lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the next line
lcd.print(“Validation Required!”);
lcd.setCursor ( 1, 1 ); // go to the next line
lcd.print(“Run Master Samples”);
lcd.setCursor ( 1, 3 ); // go to the next line
lcd.print(“Refer to WI-XXX-XX”);
delay(1000);
analogWrite(RedLED, 128);
analogWrite(GreenLED, 128);
delay(1000);
analogWrite(RedLED, 0);
analogWrite(GreenLED, 0);
}

Second half:

void NGscreen (){ lcd.clear(); ngcount++; ngcount1=(int) ngcount; math1 = ngcount / totalcount;

math2 = math1 - 1; math2=abs(math2); yield = math2 * 100;

delay(50);

lcd.setCursor ( 0, 0 ); // go to the next line lcd.print("Total:"); lcd.setCursor ( 8, 0 ); // go to the next line lcd.print(totalcount1);

lcd.setCursor ( 12, 0 ); // go to the next line lcd.print("NG:"); lcd.setCursor ( 16,0 ); // go to the next line lcd.print(ngcount1);

lcd.setCursor ( 0, 1 ); // go to the next line lcd.print("Yield:"); lcd.setCursor ( 6,1 ); // go to the next line lcd.print(yield);

lcd.setCursor ( 12,1 ); // go to the next line lcd.print("%");

lcd.setCursor ( 0, 2 ); // go to the next line lcd.print(""); lcd.setCursor ( 7, 3 ); // go to the next line lcd.print("FAIL");

if (pin1==HIGH){ lcd.setCursor ( 18,2 ); // go to the next line lcd.print("5"); }

if (pin2==HIGH){ lcd.setCursor ( 19,2 ); // go to the next line lcd.print("4"); } if (pin3==HIGH){ lcd.setCursor ( 19,3 ); // go to the next line lcd.print("1"); } if (pin4==HIGH){ lcd.setCursor ( 18,3 ); // go to the next line lcd.print("2"); }

if (pin5==HIGH){ lcd.setCursor ( 17,3 ); // go to the next line lcd.print("3"); }

if (pin6==HIGH){ lcd.setCursor ( 17,2 ); // go to the next line lcd.print("6"); }

lcd.createChar(0, customChar); lcd.setCursor(16, 2); lcd.write(byte(0)); lcd.createChar(0, customChar); lcd.setCursor(16, 3); lcd.write(byte(0)); lcd.createChar(1, customChar1); lcd.setCursor(17, 1); lcd.write(byte(1)); lcd.createChar(1, customChar1); lcd.setCursor(18, 1); lcd.write(byte(1)); lcd.createChar(1, customChar1); lcd.setCursor(19, 1); lcd.write(byte(1)); lcd.createChar(3, customChar2); lcd.setCursor(16, 1); lcd.write(byte(3));

analogWrite(redledPin, 10); // turn LED ON analogWrite(greenledPin, 0); // turn LED OFF delay(2000);

lcd.clear(); lcd.setCursor ( 0 , 0 ); lcd.print ("REJECT"); lcd.setCursor ( 1 , 1 ); lcd.print ("DROP DOWN SHOOT"); delay (5000); }

void PASSscreen (){

lcd.clear(); math1 = ngcount / totalcount;

math2 = math1 - 1; math2= abs(math2); yield = math2 * 100;

analogWrite(redledPin, 0); // turn LED OFF analogWrite(greenledPin, 50); // turn LED ON lcd.setCursor ( 0, 0 ); // go to the next line lcd.print("Total:"); lcd.setCursor ( 8,0 ); // go to the next line lcd.print(totalcount1);

lcd.setCursor ( 12, 0 ); // go to the next line lcd.print("NG:"); lcd.setCursor ( 16,0 ); // go to the next line lcd.print(ngcount1); lcd.setCursor ( 0, 1 ); // go to the next line lcd.print("Yield:"); lcd.setCursor ( 6,1 ); // go to the next line lcd.print(yield); lcd.setCursor ( 12,1 ); // go to the next line lcd.print("%"); lcd.setCursor ( 7, 4 ); // go to the next line lcd.print("PASS");

delay(250);

}

void READYscreen (){

math1 = ngcount / totalcount;

math2 = math1 - 1; math2 = abs(math2); yield = math2 * 100;

lcd.clear();

lcd.setCursor ( 0, 1 ); // go to the next line lcd.print("Yield:"); lcd.setCursor ( 6,1 ); // go to the next line lcd.print(yield); lcd.setCursor ( 12,1 ); // go to the next line lcd.print("%");

lcd.setCursor ( 0, 0 ); // go to the next line lcd.print("Total:"); lcd.setCursor ( 8,0); // go to the next line lcd.print(totalcount1);

lcd.setCursor ( 12, 0 ); // go to the next line lcd.print("NG:"); lcd.setCursor ( 16,0 ); // go to the next line lcd.print(ngcount1);

lcd.setCursor ( 0, 2 ); // go to the next line lcd.print(" READY "); lcd.setCursor ( 0, 3 ); // go to the next line lcd.print(" "); analogWrite(redledPin,0); analogWrite(greenledPin,0); }

void READpins6 (){

totalcount++; lock++; totalcount1=(int) totalcount; delay(250); pin1=digitalRead(orange); pin2=digitalRead(red); pin3=digitalRead(purple); pin4=digitalRead(blue); pin5=digitalRead(green); pin6=digitalRead(yellow); val = digitalRead(red) & digitalRead(purple) & digitalRead(green ) & digitalRead(orange) & digitalRead(blue) & digitalRead(yellow); // read input values Removed Microswitch code --> & !digitalRead(longyellow) }

void READpins5 (){

totalcount++; lock++; totalcount1=(int) totalcount; delay(250); pin1=digitalRead(orange); pin2=digitalRead(red); pin3=digitalRead(purple); pin4=digitalRead(blue); pin5=digitalRead(green); pin6=digitalRead(yellow); val = digitalRead(red) & digitalRead(purple) & digitalRead(orange) & digitalRead(blue) & digitalRead(yellow); // read input values Removed Microswitch code --> & !digitalRead(longyellow) }

void RESETcounts (){

resetbuttonstate = digitalRead(resetbutton);

if (resetbuttonstate != resetlastbuttonstate) { delay(100); resetcount++;

if (resetcount>=10){ totalcount = 0; ngcount = 0; yield= 0 ; math1 = 0; math2 = 0; math3 = 0; totalcount1=0; ngcount1=0; resetcount=0; READYscreen(); } } resetlastbuttonstate=resetbuttonstate; }

void VALcheck (){

if (val == HIGH) { // check if any inputs are HIGH () PASSscreen (); } else { NGscreen(); } }

What happened to the code tags?

the code tags are there. I couldnt fit the whole code on in 1 message. I replied to the message with the full code.

the sensor for the lock out application is called "shootsensor" and in the original post at the very end where the LCD reads "REJECT" "DROP DOWN SHOOT" i tried doing an analogRead(shootsensor) then delay (shootsensor == LOW).. Its a photosensor so its always HIGH. And is plugged into analog 9.

the code tags are there

Nope. Try again.

Is that what you meant by my code tags??

No.

Did you read this?
Why not?

I am new to the site and did not read that. But my code is very long, I think an attachment would be easiest.

LCD_working_GOD.ino (16.2 KB)

I am new to the site

That's why we nailed it to the top of every section, with the word "Please read" in bold

I get that. I'm sure I'm not the first noob to not read it either. Are you going to try to help me, or just keep getting annoyed with me??

I'm sure I'm not the first noob to not read it either.

No, you're not, nor will you be the last, sadly, but I can hope some future noob may pass this way and view this as a sort of gibbet of the digital age and pause and reflect on the potential error of their ways. I'll be getting your code shortly.

haha hopefully. Thank you!!!

pinMode(programselect, INPUT);    // declare reset button as input

I stopped reading at this point.

Either comment what the code does, or leave out the comment altogether if the code shows exactly what it does itself (it does)

Posting misleading comments wastes everyone’s time.

I appreciate your help!! I did not add the comments. Basically this code was "written", aka copy and pasted, by someone who is no longer with my company. I took this project on and had to go through the code and fix his language and trouble shoot it. The only part I am having trouble with is: At the very end of this code it has the delay (5000). I am trying to have that delay until the photosensor (shootsensor) is broken.

I was thinking:

analogRead (shootsensor) should read LOW delay (shootsensor == HIGH).

void NGscreen (){
  lcd.clear();
        ngcount++;
        ngcount1=(int) ngcount;
        math1 = ngcount / totalcount;
         
        math2 = math1 - 1;
        math2=abs(math2);
        yield = math2 * 100;
       
        delay(50);
        
        lcd.setCursor ( 0, 0 );        // go to the next line
        lcd.print("Total:");
        lcd.setCursor ( 8, 0 );        // go to the next line
        lcd.print(totalcount1);
        
        lcd.setCursor ( 12, 0 );        // go to the next line
        lcd.print("NG:");
        lcd.setCursor ( 16,0 );        // go to the next line
        lcd.print(ngcount1);
        
        lcd.setCursor ( 0, 1 );        // go to the next line
        lcd.print("Yield:");
        lcd.setCursor ( 6,1 );        // go to the next line
        lcd.print(yield);
                
        lcd.setCursor ( 12,1 );        // go to the next line
        lcd.print("%");
        
        lcd.setCursor ( 0, 2 );        // go to the next line
        lcd.print("");
        lcd.setCursor ( 7, 3 );        // go to the next line
        lcd.print("FAIL");
               

           
       
        
        
             if (pin1==HIGH){
                lcd.setCursor ( 18,2 );        // go to the next line
                lcd.print("5");
                }
   
            if (pin2==HIGH){
                lcd.setCursor ( 19,2 );        // go to the next line
                lcd.print("4");
                }
             if (pin3==HIGH){
                lcd.setCursor ( 19,3 );        // go to the next line
                lcd.print("1");
                }
             if (pin4==HIGH){
                lcd.setCursor ( 18,3 );        // go to the next line
                lcd.print("2");
                }
        
              if (pin5==HIGH){
                lcd.setCursor ( 17,3 );        // go to the next line
                lcd.print("3");
                } 
        
              if (pin6==HIGH){
                lcd.setCursor ( 17,2 );        // go to the next line
                lcd.print("6");
                } 
        
        
          lcd.createChar(0, customChar);
          lcd.setCursor(16, 2);  
          lcd.write(byte(0));
          lcd.createChar(0, customChar);
          lcd.setCursor(16, 3);  
          lcd.write(byte(0));
          lcd.createChar(1, customChar1);
          lcd.setCursor(17, 1);  
          lcd.write(byte(1));
          lcd.createChar(1, customChar1);
          lcd.setCursor(18, 1);  
          lcd.write(byte(1));
          lcd.createChar(1, customChar1);
          lcd.setCursor(19, 1);  
          lcd.write(byte(1));
          lcd.createChar(3, customChar2);
          lcd.setCursor(16, 1);  
          lcd.write(byte(3));
        
        
  
         analogWrite(redledPin, 10);  // turn LED ON
         analogWrite(greenledPin, 0);  // turn LED OFF
          delay(2000);
          
            lcd.clear();
        lcd.setCursor ( 0 , 0 );
        lcd.print ("REJECT");
        lcd.setCursor ( 1 , 1 );
        lcd.print ("DROP DOWN SHOOT");
        delay (5000);
}

Assuming you don’t mind it, one of the easiest ways to lock out a system until some event happens is something like:
Note, this could become very dangerous, as once it enters the while loop, it can’t leave it, so you will need to make sure that it escapes if it should.

uint8_t global_variable_bool = 0;

void clear_global_variable_bool(){
    global_variable_bool = 0;
}

//I am unsure how the interrupts are supposed to work, but I think it is something like:
setup(){
attachInterrupt(<PIN_FOR_PHOTO_RESISTOR, clear_global_variable_bool, LOW); //You'll need to figure out how this will work with your detector
}


loop(){
... //All of your other code here

if(condition_for_locking_out_the_system){
    global_variable_bool = 1;
    while(global_variable_bool);
}

}

PS Delay only accepts an numerical input, so delay( == <Condition); would result in a 1ms delay, as == equates to a 0 or a 1.