Sei sulla pagina 1di 4

int switchPin = 2; // switch input int motor1Pin1 = 3; // pin 2 on L293D int motor1Pin2 = 4; // pin 7 on L293D int enablePin = 9; // pin

1 on L293D

String inputString = "";

// a string to hold incoming command

boolean stringComplete = false; // whether the string is complete

void setup() { // initialize serial: Serial.begin(9600); // reserve 200 bytes for the inputString: inputString.reserve(200); // set the switch as an input: pinMode(switchPin, INPUT);

// set all the other pins you're using as outputs: pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(enablePin, OUTPUT);

// set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH);

void loop() { // if the switch is high, motor will turn on one direction: //if (digitalRead(switchPin) == HIGH) {

//} // if the switch is low, motor will turn in the opposite direction: //else { // digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D high // digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D low //} if (stringComplete) { Serial.println(inputString); // clear the string: if(inputString.equals("acw")){ digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high } else if(inputString.equals("cw")){ digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D high digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low } else if(inputString.equals("brk")){ digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D high

digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high } else{ digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low } inputString = ""; stringComplete = false; }

/* SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available. */ void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = (char)Serial.read();

// add it to the inputString: // if the incoming character is a newline, set a flag // so the main loop can do something about it: if (inChar == '\n') { stringComplete = true; }else{ inputString += inChar; } } }

Potrebbero piacerti anche