0

I would appreciate any help solving this as I have spent several hours trying to debug. My code seems to stop when I get to the initialization of the motor shield.

motorShield.begin();

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <LiquidCrystal.h>

//CREATE LCD OBJECTm
LiquidCrystal lcd = LiquidCrystal(2, 3, 4, 5, 6, 7);

//CREATE MOTOR SHIELD OBJECT
Adafruit_MotorShield motorShield = Adafruit_MotorShield();

//SET MOTOR VALUES
Adafruit_DCMotor *frontRight = motorShield.getMotor(1);
/*Adafruit_DCMotor *rearRight = motorShield.getMotor(2);
Adafruit_DCMotor *frontLeft = motorShield.getMotor(3);
Adafruit_DCMotor *rearLeft = motorShield.getMotor(4);*/

//DEFINE SENSORS
#define centerTrigPin A2
#define centerEchoPin A3

//DEFINE SENSOR VALUES
long centerDuration; // Variable for the duration of sound wave travel
int centerDistance; // Variable for the distance measurement

void setup() {
  Serial.begin(9600); 

  lcd.begin(16, 2); //Set LCD dimensions
  
  //motorShield.begin();
  
  //frontRight->setSpeed(50);
  /*rearRight->setSpeed(50);
  frontLeft->setSpeed(50);
  rearLeft->setSpeed(50);*/
  
  //frontRight->run(RELEASE);
  /*rearRight->run(RELEASE);
  frontLeft->run(RELEASE);
  rearLeft->run(RELEASE);*/

  pinMode(centerEchoPin, INPUT);
  pinMode(centerTrigPin, OUTPUT);
}
void loop() {
  //GET CENTER SENSOR
  digitalWrite(centerTrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(centerTrigPin, HIGH);
  delayMicroseconds(10);
  centerDuration = pulseIn(centerEchoPin, HIGH);
  centerDistance = centerDuration * 0.034 / 2;

  if (centerDistance <= 10){
    //SERIAL OUTPUT
    Serial.print("OBJECT: ");
    Serial.print(centerDistance);
    Serial.println(" cm");

    //LCD OUTPUT
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("OBJECT");
  }else{
    //SERIAL OUTPUT
    Serial.print("DRIVE: ");
    Serial.print(centerDistance);
    Serial.println(" cm");

    //LCD OUTPUT
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DRIVE");
  }//End Forward/Reverse Check
}//End loop

enter image description here enter image description here enter image description here

  • reduce your code to the minimum ... no LCD, no serial ... use an LED blink to indicate that the code is not frozen ... then add back serial so that you can send debug messages – jsotola Jun 25 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.