Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. /*
  2. This code will cause a TekBot connected to the AVR board to
  3. move forward and when it touches an obstacle, it will reverse
  4. and turn away from the obstacle and resume forward motion.
  5.  
  6. PORT MAP
  7. Port B, Pin 4 -> Output -> Right Motor Enable
  8. Port B, Pin 5 -> Output -> Right Motor Direction
  9. Port B, Pin 7 -> Output -> Left Motor Enable
  10. Port B, Pin 6 -> Output -> Left Motor Direction
  11. Port D, Pin 1 -> Input -> Left Whisker
  12. Port D, Pin 0 -> Input -> Right Whisker
  13. */
  14.  
  15. #define F_CPU 16000000
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include <stdio.h>
  19.  
  20. int main(void)
  21. {
  22.     DDRB = 0b11110000;
  23.     PORTB= 0b11110000;
  24.     DDRD = 0b11111100;
  25.     PORTD= 0b11111111;
  26.  
  27.     void leftHit(){
  28.         //Move forward for 2 seconds, attempting to push the object out of the way
  29.         PORTB = 0b01100000;
  30.         _delay_ms(2000);
  31.         //Move backwards for one second
  32.         PORTB = 0b00000000;
  33.         _delay_ms(1000);
  34.         //Turn left, towards the object, left motor is powered, right motor is not
  35.         PORTB = 0b00100000;
  36.         _delay_ms(1000);
  37.     }
  38.    
  39.     void rightHit(){
  40.         //Move forward for 2 seconds, attempting to push the object out of the way
  41.         PORTB = 0b01100000;
  42.         _delay_ms(2000);
  43.         //Move backwards for one second
  44.         PORTB = 0b00000000;
  45.         _delay_ms(1000);
  46.         //Turn right, towards the object, right motor is powered, left motor is not
  47.         PORTB =0b01000000;
  48.         _delay_ms(1000);
  49.     }
  50.    
  51.     void bothHit(){
  52.         //Move forward for 2 seconds, attempting to push the object out of the way
  53.         PORTB = 0b01100000;
  54.         _delay_ms(2000);
  55.         //Move backwards for one second
  56.         PORTB = 0b00000000;
  57.         _delay_ms(1000);
  58.         //Turns left, away from the object for 2 seconds
  59.         //PORTB = 0b00100000;
  60.         //_delay_ms(2000);
  61.     }
  62.    
  63.    
  64.     while (1) // loop forever
  65.     {
  66.         //make bot move forward
  67.         PORTB = 0b01100000;
  68.        
  69.         //if both are hit
  70.         //For challenge, BumpBot is already facing the object, so it will repeatedly attempt to push it out of the way
  71.         if(PIND == 0b11111100){
  72.             bothHit();
  73.         }
  74.        
  75.         //if left whisker is hit
  76.         else if(PIND == 0b11111101){
  77.             _delay_ms(70);
  78.             if(PIND == 0b11111100){
  79.                 bothHit();
  80.             }
  81.             else{
  82.                 leftHit();
  83.             }
  84.         }
  85.        
  86.         //if the right whisker is hit
  87.         else if(PIND == 0b11111110){
  88.             _delay_ms(70);
  89.             if(PIND == 0b11111100){
  90.                 bothHit();
  91.             }
  92.             else{
  93.                 rightHit();
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement