Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pins used
- const int Ain1Pin = 7;
- const int Ain2Pin = 8;
- const int PwmAPin = 9;
- const int Bin1Pin = 4;
- const int Bin2Pin = 5;
- const int PwmBPin = 6;
- void setup(){
- // Configure the pins
- // For the motor to move forward: pin 1 = HIGH and pin 2 = LOW
- // For the motor to move backward: pin 1 = LOW and pin 2 = HIGH
- // For the motor to stop or break: pin 1 = LOW and pin 2 = LOW
- // The PWM pin determines the speed of the motor
- pinMode(Ain1Pin, OUTPUT);
- pinMode(Ain2Pin, OUTPUT);
- pinMode(PwmAPin, OUTPUT);
- pinMode(Bin1Pin, OUTPUT);
- pinMode(Bin2Pin, OUTPUT);
- pinMode(PwmBPin, OUTPUT);
- }
- void loop() {
- delay(2000);
- // Move motor A forward at full speed
- digitalWrite(Ain1Pin, LOW);
- digitalWrite(Ain2Pin, HIGH);
- analogWrite(PwmAPin, 255);
- // Move motor B forward at full speed
- digitalWrite(Bin1Pin, LOW);
- digitalWrite(Bin2Pin, HIGH);
- analogWrite(PwmBPin, 255);
- delay(2000);
- // Move motor A forward at half speed
- digitalWrite(Ain1Pin, LOW);
- digitalWrite(Ain2Pin, HIGH);
- analogWrite(PwmAPin, 123);
- // Move motor B backward at full speed
- digitalWrite(Bin1Pin, HIGH);
- digitalWrite(Bin2Pin, LOW);
- analogWrite(PwmBPin, 255);
- delay(2000);
- // Move motor A backward at half speed
- digitalWrite(Ain1Pin, HIGH);
- digitalWrite(Ain2Pin, LOW);
- analogWrite(PwmAPin, 123);
- // Stop motor B
- digitalWrite(Bin1Pin, LOW);
- digitalWrite(Bin2Pin, LOW);
- analogWrite(PwmBPin, 0);
- delay(2000);
- // Stop motor A
- digitalWrite(Ain1Pin, LOW);
- digitalWrite(Ain2Pin, LOW);
- analogWrite(PwmAPin, 0);
- // Move motor B forward at half speed
- digitalWrite(Bin1Pin, LOW);
- digitalWrite(Bin2Pin, HIGH);
- analogWrite(PwmBPin, 123);
- }
- void brake () {
- // Stop motor A
- digitalWrite(Ain1Pin, LOW);
- digitalWrite(Ain2Pin, LOW);
- analogWrite(PwmAPin, 0);
- // Stop motor B
- digitalWrite(Ain1Pin, LOW);
- digitalWrite(Ain2Pin, LOW);
- analogWrite(PwmBPin, 0);
- }
- void forward () {
- // Go motor A
- digitalWrite(Ain1Pin, LOW);
- digitalWrite(Ain2Pin, HIGH);
- analogWrite(PwmAPin, 255);
- // Go motor B
- digitalWrite(Ain1Pin, LOW);
- digitalWrite(Ain2Pin, HIGH);
- analogWrite(PwmBPin, 255);
- }
- void back () {
- // back motor A
- digitalWrite(Ain1Pin, HIGH);
- digitalWrite(Ain2Pin, LOW);
- analogWrite(PwmAPin, 255);
- // back motor B
- digitalWrite(Ain1Pin, HIGH);
- digitalWrite(Ain2Pin, LOW);
- analogWrite(PwmBPin, 255);
- char Incoming_value = 0;
- Incoming_value = Serial.read(); //Read the incoming data and store it into variable Incoming_value
- Serial.print(Incoming_value); //Print Value of Incoming_value in Serial monitor
- Serial.print("\n"); //New line
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement