/* Code for open-loop motor control using potentiometer input. */ //#include //SoftwareSerial mySerial(0, 2); //RX, TX from point of view of chip. int out1; //output pin no. int out2; //output pin no. int on=500; //set the on time in microseconds or ms int off=500; //set the off time in microseconds or ms int val=0; //the input value to set speed. int dir = 0; boolean state = true; void setup() { pinMode(0, OUTPUT); // PWM pin pinMode(1, OUTPUT); // PWM pin pinMode(2, INPUT); // on/off pin pinMode(2, INPUT_PULLUP); //enable pullup resistor. pinMode(3, INPUT); // Analog in pin for speed. pinMode(4, INPUT); // direction pin. pinMode(4, INPUT_PULLUP); //enable pullup resistor. //mySerial.begin(9600); //setup serial. } void loop() { val = analogRead(3); on=val; //mySerial.println(val1); dir = digitalRead(4); state = digitalRead(2); if (dir == HIGH) { out1 = 0; //counter-clockwise out2 = 1; } else { out1 = 1; //clockwise out2 = 0; } digitalWrite(out2, LOW); digitalWrite(out1, state); delayMicroseconds(on); // Microseconds digitalWrite(out1, LOW); delayMicroseconds (off); //Microseconds }