Motor control (toggle direction with input)

This 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 =;
  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);
        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;       // turn red LED on
  delay(ledDelay);;       // turn red LED off

Do not cross-post plz