// A simple sequencer for the electromagnetic motor model int coilA = 7; // coil array pins int coilB = 6; int coilC = 5; int debug = 13; // Dubug LED, There is an LED attached to pin 13 on most arduino's int debugState = LOW; long previousMillis = 0; int tarRPM = 10; // Target RPM for Motor, Changing this value with change the motors speed int pulselength = 0; // Length of pulse needed to match target RPM, value is set in setup int coil = 0; // Stores the next coil to fire void setup(){ pinMode(coilA, OUTPUT); pinMode(coilB, OUTPUT); pinMode(coilC, OUTPUT); pinMode(debug, OUTPUT); pulselength = (6000/(tarRPM * 12)); //Sets the pulse length 6000 millis per min / (target RPM * ((# of Coils) * (# of Cores)) } void loop(){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > pulselength){ previousMillis = currentMillis; if(coil == 0) { coil = 1; digitalWrite(coilA, HIGH); digitalWrite(coilB, LOW); digitalWrite(coilC, LOW); } else if(coil == 1){ coil = 2; digitalWrite(coilA, LOW); digitalWrite(coilB, HIGH); digitalWrite(coilC, LOW); } else if(coil == 2){ coil = 0; digitalWrite(coilA, LOW); digitalWrite(coilB, LOW); digitalWrite(coilC, HIGH); } if(debugState == LOW)debugState = HIGH; else debugState = LOW; digitalWrite(debug, debugState); } }