SHARE
TWEET

Untitled

a guest Feb 26th, 2020 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //www.elegoo.com
  2.  
  3. //    The direction of the car's movement
  4. //  ENA   ENB   IN1   IN2   IN3   IN4   Description  
  5. //  HIGH  HIGH  HIGH  LOW   LOW   HIGH  Car is runing forward
  6. //  HIGH  HIGH  LOW   HIGH  HIGH  LOW   Car is runing back
  7. //  HIGH  HIGH  LOW   HIGH  LOW   HIGH  Car is turning left
  8. //  HIGH  HIGH  HIGH  LOW   HIGH  LOW   Car is turning right
  9. //  HIGH  HIGH  LOW   LOW   LOW   LOW   Car is stoped
  10. //  HIGH  HIGH  HIGH  HIGH  HIGH  HIGH  Car is stoped
  11. //  LOW   LOW   N/A   N/A   N/A   N/A   Car is stoped
  12.  
  13.  
  14.  
  15. //define L298n module IO Pin
  16. #define ENA 5
  17. #define ENB 6
  18. #define IN1 7
  19. #define IN2 8
  20. #define IN3 9
  21. #define IN4 11
  22.  
  23. //ULTRASOUND
  24. #define trigPin A5
  25. #define echoPin A4
  26. long duration;
  27. long elapsedTime;
  28.  
  29. void forward(){
  30.   digitalWrite(ENA,HIGH); //enable L298n A channel
  31.   digitalWrite(ENB,HIGH); //enable L298n B channel
  32.   digitalWrite(IN1,HIGH); //set IN1 hight level
  33.   digitalWrite(IN2,LOW);  //set IN2 low level
  34.   digitalWrite(IN3,LOW);  //set IN3 low level
  35.   digitalWrite(IN4,HIGH); //set IN4 hight level
  36.   Serial.println("Forward");//send message to serial monitor
  37. }
  38.  
  39. void back(){
  40.   digitalWrite(ENA,HIGH);
  41.   digitalWrite(ENB,HIGH);
  42.   digitalWrite(IN1,LOW);
  43.   digitalWrite(IN2,HIGH);
  44.   digitalWrite(IN3,HIGH);
  45.   digitalWrite(IN4,LOW);
  46.   Serial.println("Back");
  47. }
  48.  
  49. void left(){
  50.   digitalWrite(ENA,HIGH);
  51.   digitalWrite(ENB,HIGH);
  52.   digitalWrite(IN1,LOW);
  53.   digitalWrite(IN2,HIGH);
  54.   digitalWrite(IN3,LOW);
  55.   digitalWrite(IN4,HIGH);
  56.   Serial.println("Left");
  57. }
  58.  
  59. void right(){
  60.   digitalWrite(ENA,HIGH);
  61.   digitalWrite(ENB,HIGH);
  62.   digitalWrite(IN1,HIGH);
  63.   digitalWrite(IN2,LOW);
  64.   digitalWrite(IN3,HIGH);
  65.   digitalWrite(IN4,LOW);
  66.   Serial.println("Right");
  67. }
  68.  
  69. //before execute loop() function,
  70. //setup() function will execute first and only execute once
  71. void setup() {
  72.   Serial.begin(115200);//open serial and set the baudrate
  73.   pinMode(IN1,OUTPUT);//before useing io pin, pin mode must be set first
  74.   pinMode(IN2,OUTPUT);
  75.   pinMode(IN3,OUTPUT);
  76.   pinMode(IN4,OUTPUT);
  77.   pinMode(ENA,OUTPUT);
  78.   pinMode(ENB,OUTPUT);
  79.  
  80.   //ULTRASOUND
  81.   pinMode(trigPin, OUTPUT);
  82.   pinMode(echoPin, INPUT);
  83.  
  84. }
  85.  
  86.  
  87. //Repeat execution
  88. void loop() {
  89.   /*
  90.   forward();  //go forward
  91.   delay(1000);//delay 1000 ms
  92.   back();     //go back
  93.   delay(1000);
  94.   left();     //turning left
  95.   delay(1000);
  96.   right();    //turning right
  97.   delay(1000);
  98.   //int getstr = Serial.read();
  99.   //Serial.println(getstr);
  100. */
  101.  
  102. pinMode(echoPin, INPUT);
  103.  
  104. //ULTRASOUND
  105.  duration = pulseIn(echoPin, HIGH);
  106.   elapsedTime = millis();
  107.   //long dataArray[2] = {elapsedTime, duration} ;
  108.  
  109.   Serial.print(dataArray[0]);
  110.   Serial.print(',');
  111.   Serial.println(dataArray[1]);
  112.   digitalWrite(trigPin, LOW);
  113.   delayMicroseconds(10);
  114.   digitalWrite(trigPin, HIGH);
  115.   delayMicroseconds(5);
  116.   digitalWrite(trigPin, LOW);
  117.  
  118.  
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top