![]() ![]() Myservo. Int distance = 100 // Define an int for distance and speed NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE) ĪF_DCMotor motor1(1, MOTOR12_1KHZ) // First motor to connection 1ĪF_DCMotor motor2(3, MOTOR12_1KHZ) // Second motor to connection 3 #define MAX_SPEED 190 // Sets speed of DC motors The motors stop and reverse slightly, the servo moves left and right once to look around, and the robot turns to the left and continues to move forward until it discovers another object. ![]() The loops after that take a reading from the ultrasonic sensor and if it detects that an object is less than 15 centimeters away. The servo is given a name and attached to pin 9 on the Arduino (via the connection on the motor shield). The DC motors are defined to use connections M1 and M3 of the motor shield. The maximum distance of the ultrasonic sensor is set at 200 centimeters and the speed of the DC motors is set at a medium speed of 190 (out of 255). The Trig pin of the ultrasonic sensor is defined as Arduino A4 and the Echo pin as Arduino A5. The sketch starts by calling on the Adafruit Motor Shield, NewPing, and Servo libraries. The Servo library is already included in the IDE, so you don’t need to install it. Make sure you’ve downloaded the NewPing and Adafruit Motor Shield libraries and added them to your IDE. Program code for Obstacle avoiding robot using Arduino The complete code is provided on program code section. Once you’ve confirmed that your setup matches the above circuit diagram upload the code to the Arduino.reverse and reverse1 are not really good to distinguish. You can decide yourself, if you want to use if-statements (which are a bit easier to read or these logical statements (which are more compact).Īlso you should use better variable names. ! negates the value of reverse and & does a logical AND. In a similar way you can ditch the other if-statement with the status variable: setMotor(speed, reverse || reverse1, !reverse & reverse1) It returns true if any of both operands are true. Alternatively you can do this inline with the setMotor() call by writing: setMotor(speed, reverse || reverse1, status) Set this new variable to true in both if-statement of the buttons. Step 1: The Stuff You Need: Parts: - Arduino - Small DC motor - Wire for motor Tools: - Computer with Arduino IDE installed - A-B USB cord Step 2: Connecting the Motor Connect your DC motor to your arduino. You can set a corresponding boolean variable in the loop() function like you did with the status variable. If(enabled) analogWrite(enablePin, speed) I've written a code to check whether there's a change in the position of the encoder and according to that calculate the velocity of the motors. For example change the setMotor() function and give it also a boolean as parameter, which indicates the enabling of the motor: void setMotor(int speed, boolean enabled, boolean reverse) I'm trying to control the speed of two DC motors using an Arduino Uno and encoders that are connected to the motors. To really enable the motor with the buttons, you have add some logic for the enable pin. This is only be done with the potentiometer, which can be set to zero speed. In your code the buttons only control the direction, but are not turning the motor on or off. I just added the second button with paint, so don't get confused. Void setMotor(int speed, boolean reverse) int enablePin = 11 īoolean reverse = digitalRead(switchPin) īoolean reverse1 = digitalRead(switchPin1) The speed is controlled by the potentiometer and works fine, just the buttons are the problem. I can't switch the states and if the motor is rotating I can't stop it. the second one is for stop/start rotating on the right side.one button is to stop/start rotating on left side, and. ![]() I am trying to rotate my DC motor with two buttons: I have a problem with my Arduino project and I tried everything to fix it but nothing helps. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |