SparkFun Inventors Kit 4.0a Book Sketches files cannot find

I’m new to robotics so forgive my ignorance. I’m trying to program an autonomous car for my son’s science project. He is in 6th grade. His school gave me a SparkFun Inventors kit. The book was written in 2017. As I’m going through the exercises and cannot find the exact sketch name in the examples. Any ideas?

Bc the kit is from 2017…Here’s the old one likely for your kit (SIK 3.0, I’m guessing?)

If you do happen to have a newer kit with an older book for some reason and/or have a kit that resembles the 4.0 or later, here’s that

We do sell Hobby Gearmotor - 140 RPM (Pair) - ROB-13302 - SparkFun Electronics and Wheel - 65mm (Rubber Tire, Pair) - ROB-13259 - SparkFun Electronics if you need them

Ok I found the codes. I am trying to do project 5A (Motor Basics). I wired per the diagram and uploaded the sketch. Not working. I’m not sure what to, I have double checked everything.
One thing that I’m unsure about is selecting the correct board in Arduino. I picked Arduino Uno. It did work for project 1 (Blinking an LED).

1 Like

What are you using for a motor driver? The older kits didn’t include one. Can you post photos showing how you have things connected?

1 Like

Hi YellowDog,
First off, thank you so much for reaching out to me. I’m stressing out a little with this science project. I’m working on project 5A.
It looks like the motor controller did come with the kit.

Try setting the motor speed to 255.
Try flipping the switch on and off.

Your USB port might not provide enough power to spin the motor, try connecting the battery pack from the kit to the plug on the redboard.

The left hand edge of you picture is cut off, is the wire with the red arrow below connected?

1 Like

Hurray! It was the motor speed setting. Thank You.

YellowDog,
For project 5b, when they say enter a distance, what are the units for the distance? Can’t find it anywhere in the book.
Thanks

Inches are mentioned in the code, probably inches? There’s nothing in the kit that can accurately measure distance so anything you enter is going to be a ballpark estimate. 100 will be a further distance than 10 but any number you enter will likely be a different distance every time you enter it.

1 Like

Hello,
Is there a way you can print the sketch to a pdf or any format so we can paste on science fair poster board?
Thanks

Just print it right from the arduino IDE.
Or just copy paste the code into your favorite word processor and print it from there.

How do you print from Arduino IDE? I don’t see the command for print?

Hello YellowDog,
I uploaded the sketch SIK_Circuit_5C-AutonomousRobot. Working good so far. I was wondering if there is a speaker I can add that can play audio. I would like to the audio to play when the sensor senses an object and the robot stops for 10 seconds while playing the audio. Is that possible?
Thank You in advance for your help.

The kit came with a buzzer, you could use that to play a tune or just beep.

Hello YellowDog,
Where is the variable in the code to change the sensor distance? I have been changing the number on line 164, but it’s not consistent.

SIK_Circuit_5C-AutonomousRobot

Thank You

It’s in the example code as ‘distance’, lines 70, 76 and others SIK-Guide-Code/SIK_Circuit_5C-AutonomousRobot/SIK_Circuit_5C-AutonomousRobot.ino at master · sparkfun/SIK-Guide-Code · GitHub

/*
  SparkFun Inventor’s Kit
  Circuit 5C - Autonomous Robot

  This robot will drive around on its own and react to obstacles by backing up and turning to a new direction.
  This sketch was adapted from one of the activities in the SparkFun Guide to Arduino.
  Check out the rest of the book at
  https://www.sparkfun.com/products/14326

  This sketch was written by SparkFun Electronics, with lots of help from the Arduino community.
  This code is completely free for any use.

  View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40
  Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code
*/



//the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13;           //control pin 1 on the motor driver for the right motor
const int AIN2 = 12;            //control pin 2 on the motor driver for the right motor
const int PWMA = 11;            //speed control pin on the motor driver for the right motor

//the left motor will be controlled by the motor B pins on the motor driver
const int PWMB = 10;           //speed control pin on the motor driver for the left motor
const int BIN2 = 9;           //control pin 2 on the motor driver for the left motor
const int BIN1 = 8;           //control pin 1 on the motor driver for the left motor


//distance variables
const int trigPin = 6;
const int echoPin = 5;

int switchPin = 7;             //switch to turn the robot on and off

float distance = 0;            //variable to store the distance measured by the distance sensor

//robot behaviour variables
int backupTime = 300;           //amount of time that the robot will back up when it senses an object
int turnTime = 200;             //amount that the robot will turn once it has backed up

/********************************************************************************/
void setup()
{
  pinMode(trigPin, OUTPUT);       //this pin will send ultrasonic pulses out from the distance sensor
  pinMode(echoPin, INPUT);        //this pin will sense when the pulses reflect back to the distance sensor

  pinMode(switchPin, INPUT_PULLUP);   //set this as a pullup to sense whether the switch is flipped


  //set the motor control pins as outputs
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);

  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);

  Serial.begin(9600);                       //begin serial communication with the computer
  Serial.print("To infinity and beyond!");  //test the serial connection
}

/********************************************************************************/
void loop()
{
  //DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
  distance = getDistance();

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" in");              // print the units

  if (digitalRead(switchPin) == LOW) { //if the on switch is flipped

    if (distance < 10) {              //if an object is detected
      //back up and turn
      Serial.print(" ");
      Serial.print("BACK!");

      //stop for a moment
      rightMotor(0);
      leftMotor(0);
      delay(200);

      //back up
      rightMotor(-255);
      leftMotor(-255);
      delay(backupTime);

      //turn away from obstacle
      rightMotor(255);
      leftMotor(-255);
      delay(turnTime);

    } else {                        //if no obstacle is detected drive forward
      Serial.print(" ");
      Serial.print("Moving...");


      rightMotor(255);
      leftMotor(255);
    }
  } else {                        //if the switch is off then stop

    //stop the motors
    rightMotor(0);
    leftMotor(0);
  }

  delay(50);                      //wait 50 milliseconds between readings
}

/********************************************************************************/
void rightMotor(int motorSpeed)                       //function for driving the right motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(AIN1, HIGH);                         //set pin 1 to high
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(AIN1, LOW);                          //set pin 1 to low
    digitalWrite(AIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMA, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

/********************************************************************************/
void leftMotor(int motorSpeed)                        //function for driving the left motor
{
  if (motorSpeed > 0)                                 //if the motor should drive forward (positive speed)
  {
    digitalWrite(BIN1, HIGH);                         //set pin 1 to high
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  else if (motorSpeed < 0)                            //if the motor should drive backward (negative speed)
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, HIGH);                         //set pin 2 to high
  }
  else                                                //if the motor should stop
  {
    digitalWrite(BIN1, LOW);                          //set pin 1 to low
    digitalWrite(BIN2, LOW);                          //set pin 2 to low
  }
  analogWrite(PWMB, abs(motorSpeed));                 //now that the motor direction is set, drive it at the entered speed
}

/********************************************************************************/
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
  float echoTime;                   //variable to store the time it takes for a ping to bounce off an object
  float calculatedDistance;         //variable to store the distance calculated from the echo time

  //send out an ultrasonic pulse that's 10ms long
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  echoTime = pulseIn(echoPin, HIGH);      //use the pulsein command to see how long it takes for the
                                          //pulse to bounce back to the sensor

  calculatedDistance = echoTime / 148.0;  //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)

  return calculatedDistance;              //send back the distance that was calculated
}

If you are wanting to change how the robot responds, you can alter the other variables too (motor, turn, backup, etc)

Lines 158-on are for reporting the distance


The car is always backing up. I’m changing the numbers on lines 36 and 74.
Not understanding what line 36 is doing.
Line 66 is capturing the distance values and line 74 states if the number is less than .5 then “BackUp”. Please correct me if my thought process is incorrect.