Motor Shield - sketch assistance

Everything works on the sketch until I use the potentiometer - would someone please take a look and advise me - I am currently only using MotorA
Many thanks in advance.

typ/*************************************************************
 
  Version 1.0 Sept 2021

  Courtesy of:
  https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/
  Buttons - LC_baseTools

  Function        Channel A   Channel B
  ---------------------------------------
  Direction       Digital 12  Digital 13  //do not change motor shield
  Speed (PWM)     Digital 3   Digital 11  //do not change motor shield
  Brake           Digital 9   Digital 8   //do not change motor shield
  Current Sensing Analog  0   Analog  1   //do not change motor shield
  LED             Digital 2   Digital 3   // brake led
  Toggle          Digital 4   Digital 5   //forward/reverse (ON - OFF)
  Button          Digital 6   Digital 7   //brake
*************************************************************/
// Buttons - LC_baseTools
#include <mechButton.h>

// motor A
mechButton button1(4); // forward/reverse
mechButton button3(6); // brake

const int analogInPin1 = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin1 =A2; // Analog output pin 

int sensorValue1 = 0;        // value read from the pot
int outputValue1 = 0;        // value output to the PWM (analog out)
//----------------------------------------------------------------------------
void setup() {

  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  // pot
  pinMode(A0, INPUT);   //Motor A
  pinMode(A1, INPUT);   //Motor B

  //Led
  pinMode(2, OUTPUT);


  while (!Serial) { };
  Serial.begin(9600);

}

//----------------------------------------------------------------------------
void loop() {

  idle();// this give us multitasking - LC_baseTools

  if (button3.trueFalse()) //can we move                 
    do_BrakeON();// no brake is true            
  else
  {
    do_BrakeOFF();// yes brake is false -  we can move
    
    if (button1.trueFalse())// which way can we move
      do_Reverse(); // reverse
    else
      do_Forward(); // forward
  }



}//--------------------------------------------------------

void read_Pot()
{
  Serial.print("Reading the pot   "); Serial.println();
  // read the analog in value:
  sensorValue1 = analogRead(analogInPin1);

  // map it to the range of the analog out:
  outputValue1 = map(sensorValue1, 0, 1023, 0, 255);

  // stop any buzzing
  if (outputValue1 < 8 )outputValue1 = 0;
  if (outputValue1 > 8 )outputValue1 = 0;

  // change the analog out value:
  analogWrite(analogOutPin1, outputValue1);///////this does not seem to work


  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}
//----------------------------------------------------------------------------
void do_Forward()
{
  Serial.print("Forward  "); Serial.println();
  read_Pot();
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
//  analogWrite(3, 255);   //Spins the motor on Channel A at full speed - THIS WORKS
  
}
//----------------------------------------------------------------------------
void do_Reverse()
{
  Serial.print("Reverse "); Serial.println();
  read_Pot();
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
//  analogWrite(3, 255);   //Spins the motor on Channel A at half speed- THIS WORKS

}
//----------------------------------------------------------------------------
void do_BrakeON()
{
  Serial.print("brake ON "); Serial.println();
  digitalWrite(2, 0);  //Turn on the light -
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
}
//----------------------------------------------------------------------------
void do_BrakeOFF()
{
  Serial.print("brake OFF "); Serial.println();
  digitalWrite(2, 1);  //Turn OFF the light -
  digitalWrite(9, LOW); //Release the Brake for Channel A 
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------e or paste code here

Look at the comments at the top - Motor A uses A0 for current sensing.

Are you sure that's what you want? outputValue1 can only ever be 0 or 8?

Steve

You are correct I do not need that.
I will remove it later.
Thanks.

Oh, I will change it and see if it works.
Thanks.

wildbill -
Oh my - moved a pin on the shield to 3 and it works just fine - thanks for pointing it out.

If you can suggest any improvements - I would be grateful.
Otherwise we can consider this closed.

I recommend you change the names from 'button1' and 'button3' to DirectionAButton and BrakeAButton.

A2 is not an analog (PWM?) output pin.

DO NOT USE pinMode() ON PINS YOU ONLY USE FOR analogRead().

This is equivalent to outputValue1 = sensorValue1 / 4;

Give Pin 3 and Pin 12 meaningful names so you don't have to explain in a comment what you are doing. Note: Engaging the brakes for a few microseconds is not going to do much.

Thank you Johnwasser I will make the suggested changes and repost the sketch.
I was also going to use arrays to keep the lines of code down.

I have made most of the suggested changes and added an array to handle motor A and B.
In the process I have managed to get myself in a pickle - perhaps you could assist me a little with the logic in the callback()
Also I think I am reading the potentiometer too many times and am not too sure where or how to make the change for this.

  Throttle's for two blocks on a model railway
  Motor shield Deek-robot
  UNO R3
  Version 3.0 Sept 2021- this works for two lines from the motor
  ----------------------------------------------------------------------
  Courtesy of:
  https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/
  Buttons - LC_baseTools
  And all the Elves at Arduino
  ---------------------------------------------------------------------

  Function          Channel A   Channel B
  ---------------------------------------
  msDirection       Digital 12  Digital 13  //do not change motor shield
  msSpeed           Digital 3   Digital 11  //do not change motor shield
  msBrake           Digital 9   Digital 8   //do not change motor shield
  msSensing         Analog  14  Analog  15  //do not change motor shield

  ledBrake          Digital 2   Digital 10  //brake led
  toggleDirection   Digital 4   Digital 5   //forward/reverse
  buttonBrake       Digital 6   Digital 7   //brake
*************************************************************/

// Buttons - LC_baseTools
#include <mechButton.h>
#include <idlers.h>

const int msDirection[]  {12, 13};   // do not change motor
const int msSpeed[]      {3, 11};    // do not change motor
const int msBrake[]      {9, 8};     // do not change motor
const int msSensing[]    {14, 15};   // analog A0 and A1

const int ledBrake[]     {2, 10};    // led indicates brake on/off
const int pinCount = 2;              // number of tracks

mechButton buttonBrake[] {6, 7};     // pushbutton
mechButton toggleDirection[] {4, 5}; // toggle switch


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void setup()
{

  //Setup Channel A and B
  for (int x = 0; x < pinCount; x++)
  {
    pinMode(msDirection[x], OUTPUT);
    pinMode(msSpeed[x], OUTPUT);
    pinMode(msBrake[x], OUTPUT);
    pinMode(msSensing[x], OUTPUT);

    pinMode(ledBrake[x], OUTPUT);

    buttonBrake[x].setCallback(myCallback);    // Set up our callback.
    toggleDirection[x].setCallback(myCallback);

  }
  
  Serial.begin(9600);

}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
/*
 * This is set for motor A for testig only
 * 
 * if the brake is ON the direction cannot be moved
 * if the brake is OFF the direction can be moved
 */
void myCallback(void)
{

  bool brakeStatus[pinCount];
  int x = 0;


  Serial.print("Brake  A just became ");
  if (buttonBrake[x].trueFalse() )
  {
    Serial.println("ON");
    do_BrakeON(x);
    brakeStatus[x] == true;
  }
  else
  {
    Serial.println("OFF");
    do_BrakeOFF(x);
    brakeStatus[x] == false;
  }
  
  //..................................................
        // this is not working correctly - it should only
        // work when the brake is false.
 
    Serial.print("Direction  A just became ");
    if (toggleDirection[x].trueFalse() && brakeStatus[x]== false)
    {
      Serial.println("forward");
      do_Forward(x);
    }
    else
    {
      Serial.println("reverse");
      do_Reverse(x);
    }
  

}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void loop()
{


  idle();// this give us multitasking - LC_baseTools
  //.........................................................

}
//--------------------------------------------------------

void read_Pot(int i)
{ Serial.print("read pot  "); Serial.println();
  int sensorValue = analogRead(msSpeed[i]);
  int outputValue = sensorValue / 4;
  analogWrite(msSensing[i], outputValue);
}
//----------------------------------------------------------------------------

void do_Forward(int i)
{
  read_Pot(i);
  digitalWrite(msBrake[i], HIGH); sleep(500);
  digitalWrite(msDirection[i], HIGH);
  digitalWrite(msBrake[i], LOW);
}
//----------------------------------------------------------------------------
void do_Reverse(int i)
{
  read_Pot(i);
  digitalWrite(msBrake[i], HIGH); sleep(500);
  digitalWrite(msDirection[i], LOW);
  digitalWrite(msBrake[i], LOW);
}
//----------------------------------------------------------------------------

void do_BrakeON(int i)
{
  digitalWrite(ledBrake[i], 0);
  digitalWrite(msBrake[i], HIGH);
}
//----------------------------------------------------------------------------

void do_BrakeOFF(int i)
{
  digitalWrite(ledBrake[i], 1);
  digitalWrite( msBrake[i], LOW);
}
//----------------------------------------------------------------------------
//-----------------------------end--------------------------------------------
ype or paste code here

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.