Need help for my arduino project PLEASE ANYONE!

Ok so my project is making an arduino bingo board. Basically I have 10 lasers and 10 light sensors across from each other making a grid basically. So it basically creates a 5 by 5 grid that i guess can be touch sensitive kinda. So when you put your finger down somewhere you will disrupt 2 light sensors. One that goes up and down and one that goes left and right. When you interrupt a pair of sensors you turn on a corresponding led. I'm stuck on the programming for the light sensors. How do i make it so that when both get interrupted the led turns on and stays on until that pair gets interrupted again. PLEASE I COULD REALLY USE THE HELP! A BIG THANK YOU IN ADVANCE TO ANYONE THAT HELPS ME!

Sounds to me like you need a latch, but I won't know for certain unless you post your code. Please put your compiled sketch in code tags, of which can be obtained with the # sign above the smiley faces. If you have any errors from the compiler, please post them too.

#define ls1 7
#define ls2 8
boolean buttonState = 0;
unsigned long previousTime = 0;

void setup()
{
	Serial.begin(9600);
}

void loop()
{
	

		
	while(digitalRead(ls1) == 0  || digitalRead(ls2)) {
	print();
	}

		buttonState = (buttonState + 1) % 2;
		
		
		print();
	digitalWrite(13, buttonState);
	
	
}




 
 void print(){
	if(millis() > previousTime + 250) {
	
		Serial.println(buttonState);
		previousTime = millis();
	}
 }
 
 void offprint(){
 		if(millis() > previousTime + 250) {
	
		Serial.println("LED OFF");
		previousTime = millis();
	}
 }
 
 void onprint(){
 		if(millis() > previousTime + 250) {
	
		Serial.println("LED ON");
		previousTime = millis();
	}
 }

I'm reallyy confused on the problem with code cuz it just doesn't work as "fluent or crisp" as i want it to. Like i simply want it when both sensors are interrupted, not one, an led will turn on. I'm more worried bout getting the pairs of sensors to work. I have a shift register for the leds and stuff so ill work on that later. But i just need help on a solid program for the "sensor pair interrupttion". BTW THANK YOU FOR REPLYING!

I should have asked for you to format your code too with CTRL+T.

What do your sensors return when they are blocked? Do they return a range, or just a HIGH or LOW signal?

Have you tried to simply block one sensor to change the state of an LED? Starting small usually leads to bigger and better things.

You need to first see what the sensors return, and second use an IF statement to see if both sensors are blocked at the same time. If both are blocked LED = HIGH, else LED = LOW.

So if can use either digital inputs or analog. When using analog inputs the values are around 900 when blocked and around 200 unblocked. Digital is either 1 or 0 for blocked or unblocked. And i did try the method starting small so i used one light sensor and one laser and it worked. But i want the led to change states everytime i block the sensor. So lets say i block it and unblock it the light should turn on. Then when i block it and unblock it again it should toggle off. Get what im saying. I'm just lost on how to get the pairs working together.

if(digitalRead(sensor_1) == HIGH && digitalRead(sensor_2) == HIGH)
{
//LED = HIGH
}
else
{
//LED = LOW
}

Latching example for one button.

byte LEDpin = 13; //on-board LED
byte ButtonPin = 2; //digital pin 2

boolean button, lastState = LOW;
boolean latch = false;

void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(ButtonPin, INPUT);
}

void loop() 
{
  button = digitalRead(ButtonPin);
  if (button == HIGH && button != lastState) 
  {
    latch = !latch;
   
    if(latch == 1)
      digitalWrite(LEDpin, HIGH);
    else 
      digitalWrite(LEDpin, LOW);
      
    //latch ? digitalWrite(LEDpin, HIGH): digitalWrite(LEDpin, LOW); //Equal to above IF/ELSE statement.
  }
  lastState = button;
}

Since you will be using multiple sensors, you need to use arrays of the all sensor pins and two FOR loops to cycle through them. Make sure you make arrays for the "lastStates" too.
Sensor_Col[5] = { . . . };
Sensor_Row[5] = { . . . };

Ok so i pasted the code u put in onto a new sketch and setup only 1 sensor and laser. The problem is that it only turns the led on. When i go to retrigger it, nothing happens.

Actually sorry gimme a second. It's not giving a 0 if i cover it so i might have to check the digital reading. Ill let you know what happens.

OMG YOU ARE MY HERO! That works so fluently. So now that I have that working with just one sensor how would I implement that with another sensor. Im assuming that latch function needs to be customized for every sensor.

For two sensors, you would need to use some logic. (AND, OR, equal "==", not equal "!=", greater/less than <>)
For you, I already gave you that logic statement.

if(digitalRead(sensor_1) == HIGH && digitalRead(sensor_2) == HIGH)
{
// If both sensors are blocked, turn on LED
//LED = HIGH
}
else
{
// otherwise the LED should be turned off.
//LED = LOW
}

Ok so this is my code for the two sensors but the thing is when i take my finger out the led turns. Any way I can make it so that when I take my finger out the light stays on until i put my finger in again?

#define sensor_1 2
#define sensor_2 3
void setup()
{
	pinMode(sensor_1, INPUT);
	pinMode(sensor_2, INPUT);
	pinMode(13, OUTPUT);
}

void loop()
{
	if(digitalRead(sensor_1) == HIGH && digitalRead(sensor_2) == HIGH)
{
	digitalWrite(13, HIGH);
  // If both sensors are blocked, turn on LED
  //LED = HIGH
}
else 
{
	digitalWrite(13, LOW);
  // otherwise the LED should be turned off.
  //LED = LOW
}
}

Did you look at my latching example? It shows you how to latch a state on when the button is pressed once then off, when it is pressed again. You can do the exact same thing, but you will need to add in the variables for the second sensor.

Ok I think I did what you told me to but i probably made a couple mistakes here and there. Im just a little lost where aha. Here's my code. Dont know what i did wrong. Thanks for the continuous help. GREATLY APPRECIATED!

byte LEDpin = 13; //on-board LED
byte ButtonPin = 2; //digital pin 2
byte ButtonPin2 = 3;
boolean button, lastState = LOW;
boolean button2, lastState2 = LOW;
boolean latch = false;

void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(ButtonPin, INPUT);
  pinMode(ButtonPin2, INPUT);
}

void loop() 
{
  button = digitalRead(ButtonPin);
  button2 = digitalRead(ButtonPin2);
  if (button == HIGH && button != lastState && button2 == HIGH && button2 != lastState2) 
  {
    latch = !latch;
   
    if(latch == 1)
      digitalWrite(LEDpin, HIGH);
    else 
      digitalWrite(LEDpin, LOW);
      
    //latch ? digitalWrite(LEDpin, HIGH): digitalWrite(LEDpin, LOW); //Equal to above IF/ELSE statement.
  }
  lastState = button;
  lastState2 = button2;
}

Try this.

byte LEDpin = 13; //on-board LED
byte ButtonPin = 2; //digital pin 2
byte ButtonPin2 = 3;
boolean button, lastState = LOW;
boolean button2, lastState2 = LOW;
boolean latch = false;

void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(ButtonPin, INPUT);
  pinMode(ButtonPin2, INPUT);
}

void loop() 
{
  button = digitalRead(ButtonPin);
  button2 = digitalRead(ButtonPin2);
  if ( button != lastState && button2 != lastState2) 
  {
    if(button == HIGH && button2 == HIGH )
      latch = !latch;


    if(latch == true)
      digitalWrite(LEDpin, HIGH);
    else 
      digitalWrite(LEDpin, LOW);

    lastState = button;
    lastState2 = button2;
  }
}

Once you get this to work, you then need to put everything into arrays.

THAT WORKED PERFECTLY!!! THANK YOU SO MUCH! I may need more help in the coming days lol. Now sense im going to be needing to light up 24 leds at the most do you think I'll be able to use a shift register to help me control them all. I have one and played around with a for a little but then it got a little confusing so I went and tried to fix the problem that you just fixed for me!

A shift register will work, and you could also use the charlieplexing method too.
What shift register are you using? Take a look at the MAX7219, it may help you.

So im using the 74hc595n right now. But seems like charlieplexing might be the way to go. I'm doing some research on it now but do you think it would be easier then using shift registers. At a quick glimpse charlieplexing seems easier.

Glad I could help :slight_smile:

Im not an expert when it comes to shift registers or charlieplexing, but another user, CrossRoads may be able to help in that area.