//Window control software

int motorPin = 1;      // motor connected to digial pin 1

int analogPin = 3;   // current sensor connected to pin 3

int val = 0;           // variable to store switch position

int gen = 128;     // pwm to generate overvoltage

int genPin = 0;   //pwm pin for generating overvoltage

int swch = 2;    //input from switches on pin 2

long timeThen;

long timeNow;

long timeSince;

long maxon = 2000;       // time on before cut out

int current;                 // motor current value

int maxcurrent = 200;  //NB analog value goes DOWN as actual current goes UP! Range probably 180 to 200


void setup()


  pinMode(motorPin, OUTPUT);   // sets the pin as output

  pinMode(genPin, OUTPUT);     //sets pin as output

  pinMode(swch, INPUT);

  analogWrite(genPin, gen);   // overvoltage being generated

  analogWrite(motorPin, 0);  // window motor off


void loop()




    val = digitalRead(swch);        //wait for switch on

  }while (val == LOW);


  delay(100);  //wait for relays to close(?)

  timeThen = millis();


  for(int x = 70; x < 220; x++){    //slow start  to motor

    analogWrite(motorPin, x );




  do                                //wait for switch off or overload or time out


    val = digitalRead(swch);

    timeNow = millis();

    timeSince = timeNow - timeThen;

    current = analogRead(analogPin);


    }while (val == HIGH && timeSince < maxon && current > maxcurrent  );


   for(int x = 220; x > 70; x--){    //slow end

    analogWrite(motorPin, x );



  analogWrite(motorPin, 0 );   //turn off motor




    val = digitalRead(swch);        //wait for switch off

  }while (val == HIGH);




Using the Adafruit Trinket Circuit construction Home Julian Rogers Home

Input from either window switch is detected by pin 2.  Analog pin 3 detects the motor current. Pin 1 outputs pwm to the motor mosfet via the driver chip. Pin 0 outputs pwm to generate the 18 volts needed to drive the mosfet.

The software waits for a switch press. It briefly (about half a second) starts up the motor (slowly) without monitoring the current. It then waits for the switch to be released or an over-current or time-out (set by maxon). The motor is then slowed to a stop over about 150 MS. It then waits for the switch to be released before looping back to the start.

These values worked for me. (There is a question of whether the Trinket is running at 8 or 16 Mhz. This has a bearing on timers, I think, and I can’t quite remember how I set up the Trinket in this particular case! But see Adafruit site.)

The most important constant is maxcurrent. This has to be enough to get the window up without stalling. Note the way the circuit is connected, the value measured from  the current sensor goes down as current increases!

The next page gives some details of the construction of the circuits.

Program for Adafruit Trinket