Merging this Sketches

Im trying to combine these two sketches so that the buttons are replaced by these LINK break beam sensors and am having no luck with anything I try.

Can anyone help please?

#include "LedControl.h" // Library used for communcation with 7 segment

LedControl lc=LedControl(12,11,10,1); // (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int displayone=0;
int displaytwo=0;

// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define switchone 2
#define switchtwo 3

void setup() {
pinMode(switchone,INPUT_PULLUP);
pinMode(switchtwo,INPUT_PULLUP);

lc.shutdown(0,false); // Wake up MAX7219

lc.setIntensity(0,7); // Set brightness to medium

lc.clearDisplay(0); // Clear all displays connected to MAX7219 chip #

// Put zeros on both displays at startup

lc.setDigit(0,0,0,false); // (Max7219 chip #, Digit, value, DP on or off)
lc.setDigit(0,1,0,false);

lc.setDigit(0,2,0,false);
lc.setDigit(0,3,0,false);

}

void loop() {

// If switch 1 is clicked
if (!digitalRead(switchone)) {

displayone++; // Increase score by 1

// convert whole number to single digits
rightdigit=displayone%10;
leftdigit=displayone%100/10;

// Display extracted digits on the display
lc.setDigit(0,0,leftdigit,false);
lc.setDigit(0,1,rightdigit,false);

// Wait until switch is released to continue
while (!digitalRead(switchone)) {
}
delay(5); // Small delay to debounce the switch
}

if (!digitalRead(switchtwo)) {

displaytwo++;

rightdigit=displaytwo%10;
leftdigit=displaytwo%100/10;

lc.setDigit(0,2,leftdigit,false);
lc.setDigit(0,3,rightdigit,false);

while (!digitalRead(switchtwo)) {
}
delay(5);
}
}

/*
IR Breakbeam sensor demo!
*/

#define LEDPIN 13
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 13: Teensy 3.0 has the LED on pin 13

#define SENSORPIN 3

// variables will change:
int sensorState = 0, lastState=0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup

Serial.begin(9600);
}

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

// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (sensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}

if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
}
lastState = sensorState;
}

Show us what you tried and what the results were. "Having no luck" is not a very good problem description.

Steve

Hi, Some suggestions here:

https://arduinoinfo.mywikis.net/wiki/WritingAndUnderstandingSketches#Combining_Arduino_Sketches

slipstick:
Show us what you tried and what the results were. "Having no luck" is not a very good problem description.

Steve

Here is what I had so far, just focusing on Switch one, as Im a little unsure on "sensorState" in the sketch and how it would work with 2 if your defining for one in the original Break beam sketch.

Also, when I add Library "LedControl.h" it is dark green in the sketch, all other libraries I add are orange. I was told to ignore it, but could there be something wrong with the library I downloaded as it pertains to the sketch? maybe not running the max7219 correctly? Ledcontrol.h

#include "LedControl.h" // Library used for communcation with 7 segment

LedControl lc=LedControl(12,11,10,1); // (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int displayone=0;
int displaytwo=0;

// Variables to split whole number into single digits
int rightdigit;
int leftdigit;
int sensorState = 0, lastState=0; // variable for reading the pushbutton status

// Switches pin connection to Arduino UNO
#define switchone 2
#define switchtwo 3

void setup() {
pinMode(switchone, INPUT);
pinMode(switchtwo, INPUT);
digitalWrite(switchone, HIGH);
digitalWrite(switchtwo, HIGH);

lc.shutdown(0,false); // Wake up MAX7219

lc.setIntensity(0,7); // Set brightness to medium

lc.clearDisplay(0); // Clear all displays connected to MAX7219 chip #

// Put zeros on both displays at startup

lc.setDigit(0,0,0,false); // (Max7219 chip #, Digit, value, DP on or off)
lc.setDigit(0,1,0,false);

lc.setDigit(0,2,0,false);
lc.setDigit(0,3,0,false);

Serial.begin(9600);

}

void loop() {
sensorState = digitalRead(switchone);

// If switch 1 is clicked
if (sensorState == LOW) {

displayone++; // Increase score by 1

// convert whole number to single digits
rightdigit=displayone%10;
leftdigit=displayone%100/10;

// Display extracted digits on the display
lc.setDigit(0,0,leftdigit,false);
lc.setDigit(0,1,rightdigit,false);

// Wait until switch is released to continue
while (sensorState == LOW) {
}
delay(5); // Small delay to debounce the switch
}

if (!digitalRead(switchtwo)) {

displaytwo++;

rightdigit=displaytwo%10;
leftdigit=displaytwo%100/10;

lc.setDigit(0,2,leftdigit,false);
lc.setDigit(0,3,rightdigit,false);

while (!digitalRead(switchtwo)) {
}
delay(5);
}
}