Advertisement
olelek

First robot Arduino

Mar 18th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. //ROBOT jeżdżący
  2. //LCD I2C; 2x silniki, sonarr HC-SR04, tsop4836 RC5
  3. //Piny:
  4. /*
  5. 12: Silnik prawy kierunek;
  6. 11: Silnik prawy PWM;
  7. 4: Silnik lewy kierunek;
  8. 5: Silnik lewy PWM;
  9. 8: Sonar Echo;
  10. 9: Sonar trig;
  11. 10: TSOP4836;
  12. SDA/SCL:  LCD 16x2;
  13.  
  14. */
  15. #include <Wire.h>  // Comes with Arduino IDE
  16. #include <FastIO.h>
  17. #include <I2CIO.h>
  18. #include <LCD.h>
  19. #include <LiquidCrystal_I2C.h>
  20. #include <LiquidCrystal_SR.h>
  21. #include <LiquidCrystal_SR2W.h>
  22. #include <LiquidCrystal_SR3W.h>
  23.  
  24.  
  25.  
  26. LiquidCrystal_I2C lcd(0x20, 4, 5, 6, 0, 1, 2, 3, 7, POSITIVE);  // Set the LCD I2C address
  27. const int echo = 8;
  28. const int trig = 9;
  29. long duration, cm;
  30.  
  31. void setup(){
  32.   pinMode(6, OUTPUT);
  33.   pinMode(7, OUTPUT);
  34.   pinMode(4, OUTPUT);
  35.   pinMode(5, OUTPUT);
  36.   pinMode(8, INPUT);
  37.   pinMode(9, OUTPUT);
  38.   pinMode(10, INPUT);
  39.  
  40.   analogWrite(6,0);
  41.   analogWrite(5,0);
  42.   digitalWrite(7,LOW);
  43.   digitalWrite(4,LOW);
  44.   digitalWrite(9,LOW);
  45.  
  46.   lcd.begin(16,2);         // initialize the lcd for 20 chars 4 lines]
  47.   lcd.setBacklightPin(7, NEGATIVE);
  48.   lcd.setBacklight(0);
  49. // NOTE: Cursor Position: CHAR, LINE) start at 0  
  50.   lcd.setCursor(0,0); //Start at character 4 on line 0
  51.   lcd.print("ROBOT! 4");
  52.  
  53.  
  54. }
  55. void loop(){
  56. int wynik;
  57. lcd.setCursor(0,1);
  58. lcd.print("L:           ");
  59. lcd.setCursor(3,1);
  60. wynik = pomiar();
  61. if(wynik<3000){
  62.   lcd.print(wynik);
  63.  
  64.     if(wynik<30)
  65.     {
  66.       digitalWrite(7,LOW);
  67.       digitalWrite(4,LOW);
  68.       analogWrite(6,255);
  69.       analogWrite(5,255);
  70.     }
  71.     if((wynik>30) && (wynik<80))
  72.     {
  73.       digitalWrite(7,LOW);
  74.       digitalWrite(4,LOW);
  75.       analogWrite(6,0);
  76.       analogWrite(5,0);
  77.     }
  78.  
  79.     if(wynik>50)
  80.     {
  81.       digitalWrite(7,HIGH);
  82.       digitalWrite(4,HIGH);
  83.       analogWrite(6,0);
  84.       analogWrite(5,0);
  85.     }
  86.  
  87.  
  88. }
  89. delay(10);
  90.  
  91.  
  92. }
  93. int pomiar(void)
  94. {
  95.   pinMode(trig, OUTPUT);
  96.   digitalWrite(trig, LOW);
  97.   delayMicroseconds(2);
  98.   digitalWrite(trig, HIGH);
  99.   delayMicroseconds(10);
  100.   digitalWrite(trig, LOW);
  101.    
  102.   // Read the signal from the sensor: a HIGH pulse whose
  103.   // duration is the time (in microseconds) from the sending
  104.   // of the ping to the reception of its echo off of an object.
  105.   pinMode(echo, INPUT);
  106.   duration = pulseIn(echo, HIGH);
  107.   return microsecondsToCentimeters(duration);
  108. }
  109.  
  110. int microsecondsToCentimeters(long microseconds)
  111. {
  112. // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  113. // The ping travels out and back, so to find the distance of the
  114. // object we take half of the distance travelled.
  115. return microseconds / 29 / 2;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement