Programming multiple sensors question

Hi,
I am working on a project. I have a trash can with an IR break beam sensor. When the beam is broken, my Arduino tells a receipt printer to print out a receipt. It works great, but sometimes, if trash isn’t thrown right in the middle, it misses the sensor, so I wanted to add two more sensors. I am having trouble figuring out how to do the programming, though. I want it to print the receipt if it breaks any of the sensors. I’ve tried just doing an or statement, but that made it just continuously print receipts. This is the code I have that currently works for a single sensor. Can anyone give me any help with this? I have a mentor who normally helps me, but he is out of town.
Thank you.

#include <Adafruit_Thermal.h>
#include <SoftwareSerial.h>
#define TX_PIN 6 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer

SoftwareSerial mySerial(RX_PIN, TX_PIN);
Adafruit_Thermal printer(&mySerial);


// variables will change:
int sensorState1 = 0, lastState1 = 0, sensorState2 = 0, lastState2 = 0;       // variable for reading the pushbutton status

void setup() {
 Adafruit_Thermal printer(&mySerial, 3);
 mySerial.begin(19200);
 printer.begin(200);
 printer.wake();
 pinMode(4, INPUT);
 digitalWrite(4, HIGH); 

 Serial.begin(9600);
}

void loop() {
 // read the state of the pushbutton value:
 //sensorState = digitalRead(SENSORPIN);
 sensorState1 = digitalRead(4);
 

 // check if the sensor beam is broken
 // if it is, the sensorState is LOW:
 if (sensorState1 == LOW  && (sensorState1 != lastState1)) {
   // turn LED on:
  
   pr();

 }
 else {
  
 }

 if (sensorState1 && !lastState1) {
   Serial.println("Unbroken");
 }
 if (!sensorState1 && lastState1) {
   Serial.println("Broken");
 }
 lastState1 = sensorState1;

}

void pr() {
 spacer(2);


 printer.justify('C');
 printer.setSize('M');
 printer.println(F("Good Luck from\nDMS PANTHERBOTZ!!"));

 spacer(1);

 printer.setLineHeight(40);
 printer.justify('C');
 printer.setSize('S');
 printer.println(F("Please write your"));
 printer.println(F("NAME and TEAM"));
 printer.println(F("on the back of this ticket"));
 printer.println(F("to enter our drawing for"));
 printer.setSize('M');
 printer.println(F("PRIZES!!"));
 spacer(3);
}

void spacer(int l) {
 //printer.justify('L');
 for (int i = 0; i < l; i++) {
   printer.println();
 }
 //printer.justify('C');
}

Hello and welcome,

sensorState2 = digitalRead(5);
if ( ( sensorState1 == LOW  && (sensorState1 != lastState1) ) ||
     ( sensorState2 == LOW  && (sensorState2 != lastState2) ) )

Also maybe try IR reflect? The led light spreads out, the detector next to the led looks for reflected IR.

Really the beam break is more sure.

When you detect, that will take a small fraction of a millisecond. You need to only print once though it will detect many, many times.

If you are using an Uno then you can read 6 pins at once. Put a detector on each, no beam broken is binary 111111 = decimal 63. Anything less is beam broken. You read the pins through Port Manipulation, Uno only has 6 pins open on any port.

guix: Hello and welcome,

sensorState2 = digitalRead(5);
if ( ( sensorState1 == LOW  && (sensorState1 != lastState1) ) ||
     ( sensorState2 == LOW  && (sensorState2 != lastState2) ) )

Thank you for this. I tried putting this in my code and it just prints continuously.

GoForSmoke: Also maybe try IR reflect? The led light spreads out, the detector next to the led looks for reflected IR.

Really the beam break is more sure.

When you detect, that will take a small fraction of a millisecond. You need to only print once though it will detect many, many times.

If you are using an Uno then you can read 6 pins at once. Put a detector on each, no beam broken is binary 111111 = decimal 63. Anything less is beam broken. You read the pins through Port Manipulation, Uno only has 6 pins open on any port.

Thank you. I had thought about doing a reflect but wasn't sure I'd be able to get the angles right. I may try that if I can't get this to work. I am using an Uno. I understand what you are saying, but I'm not quite sure how to program it. I've only been working with Arduinos for about 6 weeks. If I only used 3 sensors, it would be anything less than decimal 7 is broken, right? But I'm still not sure how to code it.

mcpridgen: Thank you for this. I tried putting this in my code and it just prints continuously.

You need to set the previous state variable to the current state.

if ( ( sensorState1 == LOW  && (sensorState1 != lastState1) ) ||
     ( sensorState2 == LOW  && (sensorState2 != lastState2) ) )
  {
    // print
  }
lastState1 = sensorState1;
lastState2 = sensorState2;

If you're still having trouble, add some debug statements so you can see what your code is doing.

Edit: I moved where the last state variables are set to the current state.

A laser beam can be reflected multiple times. You could use mirrors to direct the beam through the sense aperture multiple times.

mcpridgen: Thank you. I had thought about doing a reflect but wasn't sure I'd be able to get the angles right. I may try that if I can't get this to work. I am using an Uno. I understand what you are saying, but I'm not quite sure how to program it. I've only been working with Arduinos for about 6 weeks. If I only used 3 sensors, it would be anything less than decimal 7 is broken, right? But I'm still not sure how to code it.

Yes, a 7 would mean no beam broken the way I imagine connecting it.

But it could be wired so that 0 means none broken and > 0 means beam(s) broken. That could use the same check regardless of how many beams you have (within reason).

I completely expect that you can get all the help you need whichever way you go.

Would you like to experiment with reflective sensing? I mean real simple like if the thing is detected, a led flashes.

A laser beam can be reflected multiple times. You could use mirrors to direct the beam through the sense aperture multiple times.

Two parallel mirrors, a beam could reflect at a slight angle from one end to the other. But it uses a laser out where there's people. If a mirror gets hit and tilts, someone could lose an eye. Sorry but I have a severe love/hate thing with lasers and a couple dozen lasers I got cheap!

GoForSmoke: Yes, a 7 would mean no beam broken the way I imagine connecting it.

But it could be wired so that 0 means none broken and > 0 means beam(s) broken. That could use the same check regardless of how many beams you have (within reason).

I completely expect that you can get all the help you need whichever way you go.

Would you like to experiment with reflective sensing? I mean real simple like if the thing is detected, a led flashes.

Two parallel mirrors, a beam could reflect at a slight angle from one end to the other. But it uses a laser out where there's people. If a mirror gets hit and tilts, someone could lose an eye. Sorry but I have a severe love/hate thing with lasers and a couple dozen lasers I got cheap!

Well, technically, the beam is inside a closed trash can with a flap lid, so it would be really hard for someone to lose an eye. I may try it and see what happens.

Does the lid move when something is thrown in? Sensing that would be a snap.