0

Code not working - help needed
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Code not working - help needed

by dduwstout on Sat Feb 20, 2021 2:53 am

Hello,
I am new to Adafruit motor shield V2.3. For my project (line tracing robot). I modified A.Garg's code to accommodate v2.3. I use A4 and A5 as IR sensor inputs.

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

//defining pins and variables
#define lefts A4
#define rights A5

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);


void setup() {
Serial.begin(9600);
Serial.println("Adafruit Motorshield v2 - DC Motor test!");

AFMS.begin();


myMotor->setSpeed(150);
myOtherMotor->setSpeed(150);
//declaring pin types
pinMode(lefts,INPUT);
pinMode(rights,INPUT);
}

void loop(){
//printing values of the sensors to the serial monitor
Serial.println(analogRead(lefts));
Serial.println(analogRead(rights));

//line detected by both
if(analogRead(lefts)>400 && analogRead(rights)>400){
//stop
myMotor->run(RELEASE);
myOtherMotor->run(RELEASE);
}

//line detected by left sensor
else if(analogRead(lefts)<=400 && !analogRead(rights)<=400){
//turn left
myMotor->run(BACKWARD);
myOtherMotor->run(FORWARD);
/*
myMotor->run(RELEASE);
myOtherMotor->run(FORWARD);
*/
}

//line detected by right sensor
else if(!analogRead(lefts)>400 && analogRead(rights)<=400){
//turn right
myMotor->run(FORWARD);
myOtherMotor->run(BACKWARD);
/*
myMotor->run(FORWARD);
myOtherMotor->run(RELEASE);
*/
}

//line detected by none
else if(!analogRead(lefts)<=400 && !analogRead(rights)>400){
//stop
myMotor->run(FORWARD);
myOtherMotor->run(FORWARD);
/*
myMotor->run(BACKWARD);
myOtherMotor->run(BACKWARD);
*/
}


I have no issue uploading the code. However, nothing happened (motor does not move). I run the test program from library with no issue.
Please help. Thank you in advance.

dduwstout
 
Posts: 1
Joined: Sat Feb 20, 2021 2:43 am

Please be positive and constructive with your questions and comments.