jag skulle villa laga en bil som åker åt alla håll.
Men först ska den barra kunna åka framåt och bakåt.
Och sen om det går ska den kunna åka till höger och vänster.
int input1 = 9; //motor1 int input2 = 10; //motor1 int enable1 = 11; //motor1 int input3 = 3; //motor2 int input4 = 6; //motor2 int enable2 = 8; //motor2 void setup() { pinMode(enable1, OUTPUT); pinMode(enable2, OUTPUT); pinMode(input1, OUTPUT); pinMode(input2, OUTPUT); pinMode(input3, OUTPUT); pinMode(input4, OUTPUT); digitalWrite(enable1, LOW); digitalWrite(enable2, LOW); } void loop() { // vrider motor1 medsols digitalWrite(input1, HIGH); digitalWrite(input2, LOW); digitalWrite(input3, HIGH); digitalWrite(input4, LOW); // ökar hastigheten genom PWM for(int SPEED = 0 ; SPEED <= 255; SPEED++) { analogWrite(enable1, SPEED); analogWrite(enable2, SPEED); delay(10); } delay(2000); // sänker hastigheten genom PWM for(int SPEED = 255 ; SPEED >= 0; SPEED--) { analogWrite(enable1, SPEED); analogWrite(enable2, SPEED); delay(10); } delay(2000); // vrider motorerna motsols digitalWrite(input1, LOW); digitalWrite(input2, HIGH); digitalWrite(input3, LOW); digitalWrite(input4, HIGH); // ökar hastigheten genom PWM for(int SPEED = 0 ; SPEED <= 255; SPEED++) { analogWrite(enable1, SPEED); analogWrite(enable2, SPEED); delay(10); } delay(2000); // sänker hastigheten genom PWM for(int SPEED = 255 ; SPEED >= 0; SPEED--) { analogWrite(enable1, SPEED); analogWrite(enable2, SPEED); delay(10); } delay(2000); }
nu har jag kopplat in en onesheeld som gör att jag kan svänga och köra framåt och bakåt här är koden.
/* Gamepad Shield Example This example shows an application on 1Sheeld's gamepad shield. By using this example, you can control a 2-wheel car (like the one in our warrior kit) using the gamepad in our app along with Seeedstudio's motor shield. PS: Arduino's motor shield has different configuration. OPTIONAL: To reduce the library compiled size and limit its memory usage, you can specify which shields you want to include in your sketch by defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define. */ #define CUSTOM_SETTINGS #define INCLUDE_GAMEPAD_SHIELD /* Include 1Sheeld library. */ #include <OneSheeld.h> /* A name for the LED on pin 7. */ //int ledPin = 7 ; /* Pin configuration of the Seeedstudio's motor shield. */ int motorAPin1 = 9; int motorAPin2 = 10; int motorBPin1 = 3; int motorBPin2 = 6; int motorASpeedPin = 11; int motorBSpeedPin = 8; void setup() { /* Start communication. */ OneSheeld.begin(); /* Set the LED pin as output. */ //pinMode(ledPin,OUTPUT); /* Seeedstudio's motor shield initialization. */ pinMode(motorAPin1,OUTPUT); // IN1 of motor A pinMode(motorAPin2,OUTPUT); // IN2 of motor A pinMode(motorBPin1,OUTPUT); // IN3 of motor B pinMode(motorBPin2,OUTPUT); // IN4 of motor B pinMode(motorASpeedPin,OUTPUT); // Speed of motor A pinMode(motorBSpeedPin,OUTPUT); // Speed of Motor B } void loop() { /* Turn on the LED when the red button is pressed. */ if(GamePad.isRedPressed()) { /* Turn on the LED. */ //digitalWrite(ledPin,HIGH); } else { /* Turn off the LED. */ //digitalWrite(ledPin,LOW); } /* If up is pressed, move the car forward. */ if(GamePad.isUpPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,LOW); digitalWrite(motorAPin2, HIGH); digitalWrite(motorBPin1,LOW); digitalWrite(motorBPin2, HIGH); } /* If down is pressed, move the car backwards. */ else if (GamePad.isDownPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,HIGH); digitalWrite(motorAPin2, LOW); digitalWrite(motorBPin1,HIGH); digitalWrite(motorBPin2, LOW); } /* If right is pressed, turn the car to the right. */ else if (GamePad.isRightPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,LOW); digitalWrite(motorAPin2,HIGH); digitalWrite(motorBPin1,HIGH); digitalWrite(motorBPin2,LOW); } /* If left is pressed, turn the car to the left. */ else if(GamePad.isLeftPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,HIGH); digitalWrite(motorAPin2, LOW); digitalWrite(motorBPin1,LOW); digitalWrite(motorBPin2, HIGH); } /* If nothing is pressed stop all motors. */ else { analogWrite(motorASpeedPin, 0); analogWrite(motorBSpeedPin, 0); //digitalWrite(ledPin,LOW); } }
(31.1.2018) med denna nya kod kan jag styra lys dioder en röd och en grön med min telefon
/* Gamepad Shield Example This example shows an application on 1Sheeld's gamepad shield. By using this example, you can control a 2-wheel car (like the one in our warrior kit) using the gamepad in our app along with Seeedstudio's motor shield. PS: Arduino's motor shield has different configuration. OPTIONAL: To reduce the library compiled size and limit its memory usage, you can specify which shields you want to include in your sketch by defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define. */ #define CUSTOM_SETTINGS #define INCLUDE_GAMEPAD_SHIELD /* Include 1Sheeld library. */ #include <OneSheeld.h> /* A name for the LED on pin 7. */ int ledGreen = 13 ; int ledRed = 12 ; /* Pin configuration of the Seeedstudio's motor shield. */ int motorAPin1 = 9; int motorAPin2 = 10; int motorBPin1 = 3; int motorBPin2 = 6; int motorASpeedPin = 11; int motorBSpeedPin = 8; void setup() { /* Start communication. */ OneSheeld.begin(); /* Set the LED pin as output. */ pinMode(ledGreen,OUTPUT); pinMode(ledRed,OUTPUT); /* Seeedstudio's motor shield initialization. */ pinMode(motorAPin1,OUTPUT); // IN1 of motor A pinMode(motorAPin2,OUTPUT); // IN2 of motor A pinMode(motorBPin1,OUTPUT); // IN3 of motor B pinMode(motorBPin2,OUTPUT); // IN4 of motor B pinMode(motorASpeedPin,OUTPUT); // Speed of motor A pinMode(motorBSpeedPin,OUTPUT); // Speed of Motor B } void loop() { /* Turn on the LED when the red button is pressed. */ if(GamePad.isRedPressed()) { /* Turn on the LED. */ digitalWrite(ledRed,HIGH); } else { /* Turn off the LED. */ digitalWrite(ledRed,LOW); } if(GamePad.isGreenPressed()) { /* Turn on the LED. */ digitalWrite(ledGreen,HIGH); } else { /* Turn off the LED. */ digitalWrite(ledGreen,LOW); } /* If up is pressed, move the car forward. */ if(GamePad.isUpPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,LOW); digitalWrite(motorAPin2, HIGH); digitalWrite(motorBPin1,LOW); digitalWrite(motorBPin2, HIGH); } /* If down is pressed, move the car backwards. */ else if (GamePad.isDownPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,HIGH); digitalWrite(motorAPin2, LOW); digitalWrite(motorBPin1,HIGH); digitalWrite(motorBPin2, LOW); } /* If right is pressed, turn the car to the right. */ else if (GamePad.isRightPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,LOW); digitalWrite(motorAPin2,HIGH); digitalWrite(motorBPin1,HIGH); digitalWrite(motorBPin2,LOW); } /* If left is pressed, turn the car to the left. */ else if(GamePad.isLeftPressed()) { analogWrite(motorASpeedPin, 255); analogWrite(motorBSpeedPin, 255); digitalWrite(motorAPin1,HIGH); digitalWrite(motorAPin2, LOW); digitalWrite(motorBPin1,LOW); digitalWrite(motorBPin2, HIGH); } /* If nothing is pressed stop all motors. */ else { analogWrite(motorASpeedPin, 0); analogWrite(motorBSpeedPin, 0); //digitalWrite(ledPin,LOW); } }
på den här bilden är jag nästan helt färdig.
jag blev aldrig färdig men här är en bild i alla fall.
kanske inte den bästa bilden men jag är nöjd med mitt jobb/värk.