I am a little new to the arduino space, so forgive me if this question is a bit noobish.
Below I have linked the code. I am using an Arduino Mega and using photosensors, specifically this one EE-SPY415 Reflective Photomicrosensor with a detectable sensing distance of 3- to 19-mm
I am using this with a skeeball type arcade and the Arduino is using usb to communicate with a Pi. I am not sure if this is the best way, so if someone has a better way please let me know. There are 6 sensors now and 2 more that need to be hooked up. I will be using a library that allows for more than 6 interrupts to be used, but wanted to rule out the library as being at fault.
My issue in question is when the game is started, the serial.write isn't always firing when a ball goes through the hole. I am not sure yet if the interrupt fires and not the serial write, so any help there would be good too.
Here is the ino file in question.
#include <Wire.h>
String inputString = ""; // a string to hold incoming data
boolean startGame = false;
boolean stopGame = false;
boolean readSensors = false;
boolean sensor_1_tripped = false;
boolean sensor_2_tripped = false;
boolean sensor_3_tripped = false;
boolean sensor_4_tripped = false;
boolean sensor_5_tripped = false;
boolean sensor_6_tripped = false;
#define SENSOR_1 2
#define SENSOR_2 3
#define SENSOR_3 21
#define SENSOR_4 20
#define SENSOR_5 19
#define SENSOR_6 18
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(SENSOR_1, INPUT_PULLUP);
pinMode(SENSOR_2, INPUT_PULLUP);
pinMode(SENSOR_3, INPUT_PULLUP);
pinMode(SENSOR_4, INPUT_PULLUP);
pinMode(SENSOR_5, INPUT_PULLUP);
pinMode(SENSOR_6, INPUT_PULLUP);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inputString == "start") {
startGame = true;
stopGame = false;
readSensors = true;
Serial.write("STARTED");
inputString = "";
return;
}
if (inputString == "stop") {
stopGame = true;
startGame = false;
readSensors = false;
return;
}
}
while (startGame) {
while(readSensors) {
attachInterrupt(digitalPinToInterrupt(SENSOR_1), sensor_1_callback, FALLING);
attachInterrupt(digitalPinToInterrupt(SENSOR_2), sensor_2_callback, FALLING);
attachInterrupt(digitalPinToInterrupt(SENSOR_3), sensor_3_callback, FALLING);
attachInterrupt(digitalPinToInterrupt(SENSOR_4), sensor_4_callback, FALLING);
attachInterrupt(digitalPinToInterrupt(SENSOR_5), sensor_5_callback, FALLING);
attachInterrupt(digitalPinToInterrupt(SENSOR_6), sensor_6_callback, FALLING);
if(Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inputString == "stop") {
stopGame = true;
startGame = false;
readSensors = false;
inputString = "";
Serial.write("STOPPED");
break;
}
}
if(sensor_1_tripped == true) {
sensor_1_tripped = false;
Serial.write("1");
}
if(sensor_2_tripped == true) {
sensor_2_tripped = false;
Serial.write("2");
}
if(sensor_3_tripped == true) {
sensor_3_tripped = false;
Serial.write("3");
}
if(sensor_4_tripped == true) {
sensor_4_tripped = false;
Serial.write("4");
}
if(sensor_5_tripped == true) {
sensor_5_tripped = false;
Serial.write("5");
}
if(sensor_6_tripped == true) {
sensor_6_tripped = false;
Serial.write("6");
}
}
}
}
void sensor_1_callback() {
sensor_1_tripped = true;
}
void sensor_2_callback() {
sensor_2_tripped = true;
}
void sensor_3_callback() {
sensor_3_tripped = true;
}
void sensor_4_callback() {
sensor_4_tripped = true;
}
void sensor_5_callback() {
sensor_5_tripped = true;
}
void sensor_6_callback() {
sensor_6_tripped = true;
}
Serial.read()
and also correct your code in the question (volatile and only attach in setup). It is easier to help if the problem is narrowed down as much as possible.