Advertisement
Guest User

Arduino Code

a guest
Nov 10th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. //These switches are ordered from left to right when the bot is facing away from you. SWL1 = First switch on the left side.
  2. //#define takes up less room than const int
  3. const int swL1=0; //This and the next three switches need pin numbers, but I am leaving them at 0 until I can ask Kyle my questions
  4. const int swL2=0;
  5. const int swR1=0;
  6. const int swR2=0;
  7. const int ENB=5; //Enable side B
  8. const int ENA=6; //Enable side A
  9. const int IN1=11; //Init #1
  10. const int IN2=9; //Init #2
  11. const int IN3=8; //Init #3
  12. const int IN4=7; //Init #4
  13.  
  14. //IN1-IN4 I reversed the pin numbers, because that is how they are shown on the board.
  15.  
  16. //Variable Integers:
  17. int brushSpeed=0; //The speed of the brush
  18.  
  19. //New Functions:
  20. void blockageLeft() { //In the event of an object blocking the robot's path on the left side
  21.  
  22. }
  23.  
  24. void blockageRight() { //In the event of an object blocking the robot's path on the right side
  25.  
  26. }
  27.  
  28. void lTurn(int speed,int length) { //Turn left
  29.   digitalWrite(IN1,HIGH);
  30.   digitalWrite(IN2,LOW);
  31.   digitalWrite(IN3,HIGH);
  32.   digitalWrite(IN4,LOW);
  33.   analogWrite(ENB, speed);
  34.   delay(length);
  35. }
  36.  
  37. void rTurn(int speed,int length) { //Turn right
  38.   digitalWrite(IN1,LOW);
  39.   digitalWrite(IN2,HIGH);
  40.   digitalWrite(IN3,LOW);
  41.   digitalWrite(IN4,HIGH);
  42.   analogWrite(ENA, speed);
  43.   delay(length);
  44. }
  45.  
  46. void fDrive(int speed,int length) { //Drive forwards
  47.   digitalWrite(IN1,HIGH);
  48.   digitalWrite(IN2,LOW);
  49.   digitalWrite(IN3,LOW);
  50.   digitalWrite(IN4,HIGH);
  51.   analogWrite(ENA,speed);
  52.   analogWrite(ENB,speed);
  53.   delay(length);
  54. }
  55.  
  56. void bDrive(int speed,int length) { //Drive backwards
  57.   digitalWrite(IN1,LOW);
  58.   digitalWrite(IN2,HIGH);
  59.   digitalWrite(IN3,HIGH);
  60.   digitalWrite(IN4,LOW);
  61.   analogWrite(ENA,speed);
  62.   analogWrite(ENB,speed);
  63.   delay(length);
  64. }
  65.  
  66. void setup() {
  67.   Serial.begin(9600);
  68.  
  69.   //To declare each pin an input or output
  70.  
  71.  
  72.   pinMode(IN1,OUTPUT);
  73.   pinMode(IN2,OUTPUT);
  74.   pinMode(IN3,OUTPUT);
  75.   pinMode(IN4,OUTPUT);
  76.   pinMode(ENA,OUTPUT);
  77.   pinMode(ENB,OUTPUT);
  78.  
  79.  
  80. }
  81.  
  82. void loop() {
  83.   lTurn(255,5000); //I want this command...
  84.   analogWrite(ENB, 255); //...To do the same things this and the next three things are doing.
  85.   digitalWrite(IN1, HIGH);
  86.   digitalWrite(IN2, LOW);
  87.   delay(5000);
  88.   /*But neither of these two pieces of code work properly; when I try a code that was
  89.   written for this board, it does not work either. yet I have wired all the motors
  90.   and the board as shown.*/
  91. }
  92.  
  93. /* Here is what the code from the kit tells me to enter:
  94. (For Rotation of the left motors):
  95. //www.elegoo.com
  96.  
  97. //     Left motor truth table
  98. //Here are some handy tables to show the various modes of operation.
  99. //  ENB         IN1               IN2         Description  
  100. //  LOW   Not Applicable    Not Applicable    Motor is off
  101. //  HIGH        LOW               LOW         Motor is stopped (brakes)
  102. //  HIGH        HIGH              LOW         Motor is on and turning forwards
  103. //  HIGH        LOW               HIGH        Motor is on and turning backwards
  104. //  HIGH        HIGH              HIGH        Motor is stopped (brakes)
  105.  
  106. // define IO pin
  107. #define ENB 5
  108. #define IN1 7
  109. #define IN2 8
  110.  
  111. //init the car
  112. void setup() {
  113.   pinMode(IN1, OUTPUT);     //set IO pin mode OUTPUT
  114.   pinMode(IN2, OUTPUT);
  115.   pinMode(ENB, OUTPUT);
  116.   digitalWrite(ENB, HIGH);  //Enable left motor
  117. }
  118.  
  119. //mian loop
  120. void loop() {
  121.   digitalWrite(IN1, HIGH);
  122.   digitalWrite(IN2, LOW);   //Right wheel turning forwards
  123.   delay(1000);              //delay 500ms
  124.   digitalWrite(IN1, LOW);
  125.   digitalWrite(IN2, LOW);   //Right wheel stoped
  126.   delay(1000);
  127.   digitalWrite(IN1, LOW);
  128.   digitalWrite(IN2, HIGH);  //Right wheel turning backwards
  129.   delay(1000);
  130.   digitalWrite(IN1, HIGH);
  131.   digitalWrite(IN2, HIGH);  //Right wheel stoped
  132.   delay(1000);
  133. }
  134.  
  135. This was just for the left motors, the right motors are similar.
  136. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement