Simple Car

I am working towards a big project by completing it in smaller parts, at the moment, I'm just making two motors that go forward at a speed set by a potentiometer when a button is being pressed. I'm using the Adafruit motor shield I'm having some problems, these problems are extremely strange, the motor stays on for seemingly random periods of time and the potentiometer doesn't seem to be working. I was looking for troubleshooting but it's also possible that my wiring is bad because the Adafruit motor shield just has holes and wires stay in horribly.

Thanks

Here's my coded

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *leftMotor = AFMS.getMotor(1);
Adafruit_DCMotor *rightMotor = AFMS.getMotor(2);

int potPin = 2;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor
const int BUTTON = 5; //corresponds to button used to toggle motor
const int BUTTON2 = 12; //corresponds to button used to toggle servo

void setup() 

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

pinMode(BUTTON, INPUT);

}

void loop() 

{ 
  
val = analogRead(potPin);    
bool pressingButton = (digitalRead(BUTTON) == HIGH) ? true : false;
if(pressingButton == true)
 {
  
 leftMotor->setSpeed(val); //set the speed
 leftMotor->run(FORWARD); //set the run direction
 rightMotor->setSpeed(val); //set the speed
 rightMotor->run(FORWARD); //set the run direction
 //leftMotor->run(RELEASE); //turn on motor
 
 }
 
 else
 
 {

  leftMotor->run(RELEASE);
  rightMotor->run(RELEASE);
  
 }
}

By "holes", do you mean the through-plated solder pads that are designed to solder pins to? Or do you mean pin sockets? Can you be more specific, or post a photo?

bool pressingButton = (digitalRead(BUTTON) == HIGH) ? true : false;
if(pressingButton == true)

If you remember that HIGH==1==true and LOW==0==false you can simplify this to:

bool pressingButton = digitalRead(BUTTON);
if(pressingButton)

johnwasser:

bool pressingButton = (digitalRead(BUTTON) == HIGH) ? true : false;

if(pressingButton == true)



If you remember that HIGH==1==true and LOW==0==false you can simplify this to:


bool pressingButton = digitalRead(BUTTON);
if(pressingButton)

Oh right, thanks

aarg:
By "holes", do you mean the through-plated solder pads that are designed to solder pins to? Or do you mean pin sockets? Can you be more specific, or post a photo?

Here's a picture, it's not mine, those holes just below/above the soldered ones are what I'm referring to, I'm not sure of the technical term

Imgur

Did you try soldering to them? Only the Female Headers are meant to hold wires without soldering, and even those work best with crimp terminals on the wires.
Crimp on your own

and slide into housing for separation, singly or in groups (groups are more secure)
https://www.pololu.com/product/1900
Or use terminated wires. Can buy them long, and cut in half to connect to your stuff

Has anyone looked at my code to see if that is the source of my problems? I am looking into buying some crimp terminals though

Here's an updated version I've made a couple of modifications

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *leftMotor = AFMS.getMotor(1);
Adafruit_DCMotor *rightMotor = AFMS.getMotor(2);

int potPin = 2;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor
const int BUTTON = 5; //corresponds to button used to toggle motor
const int BUTTON2 = 12; //corresponds to button used to toggle servo

void setup() 

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

pinMode(BUTTON, INPUT);

}

void loop() 

{ 
  
val = analogRead(potPin);    
bool pressingButton = (digitalRead(BUTTON) == HIGH) ? true : false;
if(pressingButton == true)
 {
  
 leftMotor->setSpeed(val / 4); //set the speed
 leftMotor->run(FORWARD); //set the run direction
 rightMotor->setSpeed(val / 4); //set the speed
 rightMotor->run(FORWARD); //set the run direction
 //leftMotor->run(RELEASE); //turn on motor
 
 }
 
 else
 
 {

  leftMotor->run(RELEASE);
  rightMotor->run(RELEASE);
  
 }
}