Motor control (toggle direction with input)

The code below is what I have so far. 2 things I notice (that need fixing).

  1. The motor does not run initially until I trigger the sensor.
  2. Motor speed does not change on the fly, it only changes upon the next time the sensor is triggered.

I’m a little lost at how to fix these 2, some pointers would help. Thanks in advance.

Hardware I’m using is the Baby Orangutan 328P. (With some of their libraries).

#include <OrangutanLEDs.h>
#include <OrangutanAnalog.h>
#include <OrangutanMotors.h>
 
OrangutanAnalog analog;
OrangutanLEDs leds;
OrangutanMotors motors;
const int sensorPin = 0; // the sensor pin 
int oldsensorState;  
int motorDirection = 0;

void setup()               // run once, when the sketch starts
{
 pinMode(sensorPin, INPUT_PULLUP); 
 oldsensorState = digitalRead(sensorPin); 
}
 
void loop()                // run over and over again
{

  
  // note that the following line could also be accomplished with:
  // int pot = analog.read(7);
  int pot = analog.readTrimpot();    // determine the trimpot position
  int motorSpeed = pot/2-256;  // turn pot reading into number between -256 and 255
  if(motorSpeed == -256)
    motorSpeed = -255; // 256 is out of range
    
  int newsensorState = digitalRead(sensorPin);
   if (newsensorState != oldsensorState) {          // the button state has changed!
    if (newsensorState == LOW) {                // check if the button is pressed
      motorDirection = !motorDirection;  // Reverse direction
      if (motorDirection)
        motors.setSpeeds(motorSpeed, motorSpeed);
      else
        motors.setSpeeds(-motorSpeed, -motorSpeed);
    }
  }
  
   
  int ledDelay = motorSpeed;
  if(ledDelay < 0)
    ledDelay = -ledDelay;  // make the delay a non-negative number
  ledDelay = 256-ledDelay; // the delay should be short when the speed is high
 
  leds.red(HIGH);       // turn red LED on
  delay(ledDelay);
 
  leds.red(LOW);       // turn red LED off
  delay(ledDelay);
}

li_gangyi: 1. The motor does not run initially until I trigger the sensor.

Your code only calls motors.setSpeeds() when the button state changes. So why would you expect it to run initially?

  1. Motor speed does not change on the fly, it only changes upon the next time the sensor is triggered.

Your code only calls motors.setSpeeds() when the button state changes.

Your code does exactly what you've told it to do. If that's not what you want, you need to modify it ;)

  1. Motor speed does not change on the fly, it only changes upon the next time the sensor is triggered.

Your code only calls motors.setSpeeds() when the button state changes.

Your code does exactly what you've told it to do. If that's not what you want, you need to modify it ;) [/quote]

I'm having problems understanding how to do this, do I call out analog.readTrimpot() in the loop (for the button state change). It's been awhile since I've coded anything so I'm very rusty.

If you want to set the motor speeds at startup then you need to insert code to do that. As a hint, the setup() function is called once at startup and setSpeeds() seems to be what you would call to set the speeds.