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:


  // reserve 200 bytes for the inputString:

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;
      inputString = "";
    if (inputString == "stop") {
      stopGame = true;
      startGame = false;
      readSensors = false;

  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 = "";

      if(sensor_1_tripped == true) {
        sensor_1_tripped = false;
      if(sensor_2_tripped == true) {
        sensor_2_tripped = false;
      if(sensor_3_tripped == true) {
        sensor_3_tripped = false;
      if(sensor_4_tripped == true) {
        sensor_4_tripped = false;
      if(sensor_5_tripped == true) {
        sensor_5_tripped = false;
      if(sensor_6_tripped == true) {
        sensor_6_tripped = false;

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;
  • 3
    Why do you use attachInterrupt() every time sensors are read? Is not it enough to do it only at the beginning of the game?
    – AltAir
    Nov 2, 2017 at 19:53
  • 4
    Those booleans that are used in the ISR and are also read from loop need to be volatile.
    – Delta_G
    Nov 2, 2017 at 19:57
  • I added volitile but it didn't help (I did read about how that needed to be done though). As for the attachInterrupt I have it now in the setup() but if its already attached it ignores it. Either way the interrupts still arent working as expected.
    – Fyb3roptik
    Nov 2, 2017 at 22:09
  • Could you try to test with a simpler code without any 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. Oct 29, 2018 at 22:06
  • I ended up using Android Things GPIO library and built a custom board that plugs into the Pi via the UART
    – Fyb3roptik
    Oct 31, 2018 at 15:38

1 Answer 1


The best solution I found for communicating interrupts to the Pi3 from Arduino is by using the GPIO. Android Things has a library for this and it all works without any lag!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.