Light sensor code Seems to be causing a Motoron motorsheild error

Hello everyone,
I have been making a mini Sumo-bot for competition coming up soon. I have been trying to code QRE1113 Analog line sensors to detect the white ring around the sumo arena to prevent my robot driving off the edge. To drive the motors I have been using the Pololu Motoron M3S256 triple motor controller.
I have been working off of the I2Csimple e.g. code (which works from default ) from the#include <Motoron.h> library, but when I add the line sensor readings to the void_loop the code the motor controller stops working, the error light coming on and the status light blinking once per second on the motor controller. Despite this, the code complies in the IDE fine. I believe the problem is in the void_loop with the line sensor code with the problem being when I tell the board to assign the values to the variables. Even though this happens to the motors the line sensors still give me constant readings when I use Serial.print(LS2_read); etc. I am fairly new to ardiuno, so if I have overlooked something obvious don't be surprised.

I have checked all the connections on senors a few times so I don't think it is that, but if that becomes the only answer I can check again

here is the example code from Motoron that works

#include <Motoron.h>

MotoronI2C mc;

void setup()
{
  Wire.begin();

  // Reset the controller to its default settings, then disable
  // CRC.  The bytes for each of these commands are shown here
  // in case you want to implement them on your own without
  // using the library.
  mc.reinitialize();    // Bytes: 0x96 0x74
  mc.disableCrc();      // Bytes: 0x8B 0x04 0x7B 0x43

  // Clear the reset flag, which is set after the controller
  // reinitializes and counts as an error.
  mc.clearResetFlag();  // Bytes: 0xA9 0x00 0x04

  // By default, the Motoron is configured to stop the motors if
  // it does not get a motor control command for 1500 ms.  You
  // can uncomment a line below to adjust this time or disable
  // the timeout feature.
  // mc.setCommandTimeoutMilliseconds(1000);
  mc.disableCommandTimeout();

  // Configure motor 1
  mc.setMaxAcceleration(1, 140);
  mc.setMaxDeceleration(1, 300);

  // Configure motor 2
  mc.setMaxAcceleration(2, 200);
  mc.setMaxDeceleration(2, 300);

  
}

void loop()
{
  if (millis() & 2048)
  {
    mc.setSpeed(1, 800);
    mc.setSpeed(2, -800);
  }
  else
  {
    mc.setSpeed(1, -800);
    mc.setSpeed(2, 800);
  }

 
}

Here is my code that doesn't work. This code is meant to recognize when either back 2 line sensors read less than the white int and make the robot move forward, away from the edge.(have not added the rest as this didn't work)

#include <Motoron.h>

MotoronI2C mc;
int White = 700;
int Black = 800;
int LS2_read;       // A2 reading
int LS3_read;       // A3 reading
int LS4_read;       // A4 reading 
int LS5_read;       // A5 reading

#define LS2 A2      //Defineing Line senor pins
#define LS3 A3
#define LS4 A4
#define LS5 A5
void setup()
{
  Wire.begin();

  // Reset the controller to its default settings, then disable
  // CRC.  The bytes for each of these commands are shown here
  // in case you want to implement them on your own without
  // using the library.
  mc.reinitialize();    // Bytes: 0x96 0x74
  mc.disableCrc();      // Bytes: 0x8B 0x04 0x7B 0x43

  // Clear the reset flag, which is set after the controller
  // reinitializes and counts as an error.
  mc.clearResetFlag();  // Bytes: 0xA9 0x00 0x04

  // By default, the Motoron is configured to stop the motors if
  // it does not get a motor control command for 1500 ms.  You
  // can uncomment a line below to adjust this time or disable
  // the timeout feature.
  // mc.setCommandTimeoutMilliseconds(1000);
   mc.disableCommandTimeout();

  // Configure motor 1
  mc.setMaxAcceleration(1, 140);
  mc.setMaxDeceleration(1, 300);

  // Configure motor 2
  mc.setMaxAcceleration(2, 140);
  mc.setMaxDeceleration(2, 300);

  

}

void loop()
{
  LS2_read= analogRead(LS2);
  LS3_read= analogRead(LS3);
  LS4_read= analogRead(LS4);
  LS5_read= analogRead(LS5);
  

  if (LS4_read < White || LS5_read < White) {
    mc.setSpeed (1, 800);
    mc.setSpeed(2, -800);
  }
  else
  {
    mc.setSpeed(1, -800);
    mc.setSpeed(2, 800);
  }
  delay(10);

Any suggestions would be great as I have been stuck for a couple of days on this
My specs are:

  • Arduino Uno R4 wifi
  • M3S256 Motoron motor controller
  • 2x yellow geared 12V DC motors
  • 2x rechargeable 9V batteries in parallel connected to the motor controller
  • 4x QRE1113 Analog line sensors in pins A2, A3, A4, A5
  • solder-less breadboard

@awesomeguy01355

You can't use A4 and A5, they are also the I2C pins used by the motor
Use A0, A1, A2, A3 for the line sensor.

1 Like

Hi, @awesomeguy01355
Welcome to the forum.

Thanks for using code tags. :+1:

Did you write some code that just read the sensors, no motor controller involved.

If not then forget your code for a while and get JUST the sensor inputs working.
Use Serial.print and the IDE monitor feature to make sure you are reading the sensors.

Can you please post specs/data links to the sensor and the motor controller?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Here is death to the Uno i2c bus. Pins A4 and A5 are the i2c bus pins for the motor controller.

Edit: Never mind. jim_p beat me to it (as usual). :slight_smile:

1 Like

Thank you, I will try changing the Analog pins

It worked, thank you so much I have been stuck here for days!!!!!!

Glad I could help
Have fun!

1 Like

Very bad idea!!
Never connect batteries in parallel!!

1 Like

Oh! What problems will I cause if I do?

First let me ask if you connected the line sensors like I described in post #2

yes I did and it stated working as intended

Well one battery can discharge into the other and your batteries will drain much faster than you think.
If the discharge current from one battery to the other is high enough the batteries could get hot, swell and discharge toxic gas. If it's really bad they could explode.

1 Like

Thank you for telling me, I will make sure I fix it. Any suggestions for increasing the run time of the robot without running parallel?

Since your motors are 12V, I would use one 3S Li-ion battery (11.1V) or three 3.7V Li-ion batteries in series (11.1V).
It should go a little faster with 11V.

Thanks for the ideas, I will have a look at the online for some 3S batteries. I have got one for a RC car but it is too big.

This looks like a good deal.
They are fairly small and have a high discharge rate.

These are a little smaller and still have a high discharge rate

You need to shop around but make sure you use ones with a high peak discharge rate (1A or more). It will make your robot accelerate much faster.

Thanks for the info. I will have a good look online, but that is a good place to start. Thank you for all the help.