Problem with schield (M2 and sensor doesn't work when M1 motor is working)

I am having a trouble with letting my motor and sensor work when I let the programma work :frowning: It seems like there is no error to motor since I switch M1 and M2 motor… So it was obviously something to do with M2. On the other hand, I tried to see if something will change motor’s direction (by using sensor) but it doesn’t seem to work either :frowning: please help me!

this is my code :slight_smile:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_MS_PWMServoDriver.h”
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which ‘port’ M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
int analogPin = A0; // IR
int valIR = 0;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Adafruit Motorshield v2 - DC Motor test!”);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz

// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor->setSpeed(150);
myMotor->run(FORWARD);
// turn on motor
myMotor->run(RELEASE);
// Set the speed to start, from 0 (off) to 255 (max speed)
myOtherMotor->setSpeed(150);
myOtherMotor->run(FORWARD);
// turn on motor++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++65
myOtherMotor->run(RELEASE);
}
void loop() {
uint8_t i;
valIR = analogRead(A0);
Serial.println(valIR);
if(valIR>200) {
Serial.print(“achteruitIR”);/backward/
myMotor->run(FORWARD);
myOtherMotor->run(RELEASE);
for (i=0; i<150; i++) {
delay(10);
myMotor->setSpeed(i);
}
}
else {
Serial.print(“vooruit”); /forward/
myMotor->run(BACKWARD);
myOtherMotor->run(FORWARD);
for (i=255; i<255; i++) {
myMotor->setSpeed(i);
myOtherMotor->setSpeed(i);
}
for (i=255; i!=255; i–) {
delay(10);
myMotor->setSpeed(i);
myOtherMotor->setSpeed(i);
}
}
}

Please use code tags.

Read this before posting a programming question

How to use this forum