Adapting Toy Car Speed Checker to Arduino Uno instead of Feather M0 Basic Proto

Hi, I am a novice in electronics. I am trying to follow a tutorial to build a toy car speed checker for my little brother (this one: HERE), but I am using arduino uno instead of adafruit feather since I have it already. the same IR sensors shown in the tutorial are not available where I live, so I am using 2 phototransistors to receiver IR beam. The code I used is this one:

#include <SPI.h>
#include <Wire.h>

#define SENSORPIN1 10 //Sensor 1 is on the right
#define SENSORPIN2 11 //Sensor 2 is on the left

long end_time; // When Sensor 2 is triggered
long start_time; // When Sensor 1 is triggered
long elapsed_time; // End time minus start time

float mph; // Speed calculated

int trigger1 = 0; // Sensor 1
int trigger2 = 0; // Sensor 2
int sensor1State; // Sensor 1 status
int sensor2State; // Sensor 2 status

void setup() {
  Serial.begin(9600);
  pinMode(SENSORPIN1, INPUT); // Sensor 1 as input
  digitalWrite(SENSORPIN1, HIGH); // Turn on the pullup
  pinMode(SENSORPIN2, INPUT); // Sensor 2 s input
  digitalWrite(SENSORPIN2, HIGH); // Turn on the pullup

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)

}

// Function to determine speed
void speed()
{
  // subtract end time from start time to get total time
  elapsed_time = ((end_time - start_time));

  // convert mm/s to mph
  mph = ((106000 / elapsed_time) * 0.0022);
  
 Serial.print(mph);
 Serial.print("mph");

}

void loop() {
  // Read the state of the IR sensor 1:
  sensor1State = digitalRead(SENSORPIN1);

  // See if IR beam of sensor 1 has been broken
  if (sensor1State == LOW) {

    // Check to make sure both sensors have not triggered
    if (trigger1 == 0 && trigger2 == 0) {

      // Save time when sensor 1 was triggered
      start_time = millis();

      // Prevent sensor 1 from triggering again
      trigger1 = 1;
    }
  }

  // Read the state of the IR sensor 2:
  sensor2State = digitalRead(SENSORPIN2);

  // See if IR beam of sensor 2 has been broken
  if (sensor2State == LOW) {

    // Check to make sure sensor 1 has triggered but not sensor2
    if (trigger2 == 0 && trigger1 == 1) {

      // Save time when sensor 2 was triggered
      end_time = millis();

      // Run speed function
      speed();

      // Prevent sensor 2 from triggering again
      trigger2 = 1;
    }
    delay(1000);

    // Reset both sensors
    trigger1 = 0;
    trigger2 = 0;
  }
}

I have also tried to adapt the circuit as best as I can after a lot of research. I used 330 ohms resistors for IR transmitters, and 10K ohms for the IR phototransistors.

The changes I made to the code is deleting <Adafruit_GFX.h> and <Adafruit_SSD1306.h> libraries, and all the commands related to the screen display and replaced them with serial.print().

The problem is I cant get anything to show up on the serial monitor and now I have been stuck for 3 days. Did I make a mistake in wiring the circuit? or in changing the code?

Time to brush up on debugging skills.

At the beginning of "loop()", add a line such as "Serial.println("begin loop");".

At the beginning of :"speed()", add a line such as "Serial.println("begin speed");'

Do the same exercise until you can spot the problem and correct it.

Paul

Hi Paul, thanks a lot for your reply. I did what you have suggested and found that the IR beam does not break when I put my hand between the transmitter and the phototransistor. Is there a way to properly block it? Because I found a lot of IR break beam projects on youtube and they seem to block it easily by putting their hands across the transmitter and receiver.

Could it be the surroundings?

Could it be the surroundings?

There could be enough IR in the ambient light to swamp the phototransistor. Put the phototransistor in the top, facing down and put it recessed in a short tube to block "off center" light. The alternative, if that does not work, is to use a specialized receiver and pulsed IR light.

Do you have a DMM (multimeter)? Check the voltage from phototransistor collector to ground as you wave your hand through the beam. Put your finger right over the phototransistor to block it completely, Does the voltage change? If you don't have a meter, connect the output of a phototransistor to an analog input and write a short program that reads the input and prints to serial monitor. Do the values change when you block the beam? Post your results, please.

Make sure that the emitter is emitting. Use your cell phone camera to watch the LEDs while cycling the power. Do you see the LEDs going on and off?

edit:
I just noticed that your wiring is wrong in the posted diagram. The 10K resistors are in series between the input and phototransistor output and not necessary and may cause problems.

I'd recommend using say one of the 8bit timers to generate a modulated IR pulse and use a TSOP IR receiver to detected the beam break.

For example: TSOP4038 - Continuous burst IR reciever at 38KHz

You could then use the Timer0 pin (That is pin 6 on the UNO) of the UNO to modulate an IR (940nm) LED at 38KHz.

Put the receiver and LED next to each other...but separated with a dark material (best to set the IR led in a tube for example). The IR receiver will go LOW when it detected the reflected busts of 38KHz IR from the car.

You can place this in your setup() to start the pulsing the LED on Pin 6 at 38KHz (assuming arduino UNO):

pinMode(6,OUTPUT); //Set IR LED as output...DDRD|=(1<<PD6);
TCCR0A = 0;
TCCR0A |= (1<<COM0A0)|(1<<WGM01); // Toggle pin 6 on match...CTC mode.

TCCR0B = 0;
TCCR0B |= (1<<CS00)|(1<<CS01); //Divide 16MHz by 8 = ~200,000KHz

OCR0A = 53;  //The "Compare" register. 53 clock cycles @ 200,000KHz = ~38KHz.