Advertisement
Guest User

Untitled

a guest
Feb 4th, 2015
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. #include <MakeItRobotics.h>
  2. MakeItRobotics line_following;
  3.  
  4. #include <IRremote.h>
  5. #include <IRremoteInt.h>
  6.  
  7. #include "pitches.h"
  8. #include <NewTone.h>
  9.  
  10. //ultra sonic
  11. #include "Arduino.h"
  12.  
  13. #define MOVE_FORWARD 0xE0E006F9
  14. #define MOVE_RIGHT 0xE0E046B9
  15. #define MOVE_LEFT 0xE0E0A659
  16. #define STOP_MOVE 0xE0E016E9
  17. #define INC_TURN_RATE 0xE0E048B7
  18. #define DEC_TURN_RATE 0xE0E008F7
  19. #define MOVE_BACKWARD 0xE0E08679
  20. #define DANCE 0xE0E0F00F
  21.  
  22. #define SL_BD_RT 9600
  23. #define RECV_PIN 11
  24. #define RED_LED 5
  25.  
  26. #define MOVE_COUNT_MAX 1
  27. #define MOVE_INTERVAL 1000
  28. #define DONT_MOVE_COUNT_MAX 3
  29.  
  30. IRrecv irrecv(RECV_PIN);
  31. decode_results results;
  32.  
  33. int ledState = LOW;
  34. long previousMillis = 0;
  35. long interval = 250;
  36.  
  37. int move_count = 0;
  38. int dont_move_count = 0;
  39. long movePreviousMillis = 0;
  40.  
  41. int ultrasonicCount = 0;
  42. int ultrasonicCountMax = 1000;
  43.  
  44. bool movingForward = false;
  45. bool movingLeft = false;
  46. bool movingRight = false;
  47. bool movingBackward = false;
  48.  
  49. int robot_move_speed = 255;
  50. bool freewill = false;
  51. bool findingRoute = false;
  52.  
  53. int stoppedCount = 0;
  54. int stoppedCountMax = 10000;
  55.  
  56. int turn_rate = 150;
  57.  
  58. int mario_theme_melody[] = {zg,za,zb,zy,zg,za,zb,zy,zx,zz,zC,zD,zx,zz,zC,zD,za,zb,zy,zw,za,zb,zy,zw,zz,zC,zD,zE,zz,zC,zD,zE,zb,zy,zw,zF,zC,zD,zE,zq,zy,zw,zF,zG,zD,zE,zq,zi,0,0,0,0,0,0,0,0,za,zz,zb,zC};
  59.  
  60. int mario_theme_noteDurations[] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};
  61.  
  62. int mario_theme_melody2[] = {NOTE_G4, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_DS4,
  63. NOTE_F4, NOTE_G4, NOTE_F4, NOTE_G4};
  64.  
  65. int mario_theme_noteDurations2[] = {8,8,8,4,4,4,8,8,2};
  66.  
  67. class Ultrasonic
  68. {
  69. public:
  70. Ultrasonic(int pin);
  71. void DistanceMeasure(void);
  72. long microsecondsToCentimeters(void);
  73. long microsecondsToInches(void);
  74. private:
  75. int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger.
  76. long duration;// the Pulse time received;
  77. };
  78. Ultrasonic::Ultrasonic(int pin)
  79. {
  80. _pin = pin;
  81. }
  82. /*Begin the detection and get the pulse back signal*/
  83. void Ultrasonic::DistanceMeasure(void)
  84. {
  85. pinMode(_pin, OUTPUT);
  86. digitalWrite(_pin, LOW);
  87. delayMicroseconds(2);
  88. digitalWrite(_pin, HIGH);
  89. delayMicroseconds(5);
  90. digitalWrite(_pin,LOW);
  91. pinMode(_pin,INPUT);
  92. duration = pulseIn(_pin,HIGH);
  93. }
  94. /*The measured distance from the range 0 to 400 Centimeters*/
  95. long Ultrasonic::microsecondsToCentimeters(void)
  96. {
  97. return duration/29/2;
  98. }
  99. /*The measured distance from the range 0 to 157 Inches*/
  100. long Ultrasonic::microsecondsToInches(void)
  101. {
  102. return duration/74/2;
  103. }
  104.  
  105. Ultrasonic ultrasonic(7);
  106.  
  107. void setup()
  108. {
  109. pinMode(RED_LED, OUTPUT);
  110.  
  111. delay(500);
  112.  
  113. //used for motors
  114. Serial.begin(10420);
  115. //used for testing & ultrasonic
  116. //Serial.begin(SL_BD_RT);
  117.  
  118. line_following.line_following_setup();
  119. line_following.all_stop();
  120. irrecv.enableIRIn();
  121. }
  122.  
  123. void loop()
  124. {
  125. long RangeInInches;
  126. long RangeInCentimeters;
  127. ultrasonicCount++;
  128. if(ultrasonicCount >= ultrasonicCountMax)
  129. {
  130. ultrasonicCount = 0;
  131.  
  132. ultrasonic.DistanceMeasure();// get the current signal time;
  133. RangeInInches = ultrasonic.microsecondsToInches();//convert the time to inches;
  134. /* RangeInCentimeters = ultrasonic.microsecondsToCentimeters();//convert the time to centimeters
  135. Serial.println("The distance to obstacles in front is: ");
  136. Serial.print(RangeInInches);//0~157 inches
  137. Serial.println(" inch");
  138. Serial.print(RangeInCentimeters);//0~400cm
  139. Serial.println(" cm");*/
  140.  
  141. if(freewill == true)
  142. {
  143. if(RangeInInches <= 5)
  144. {
  145. if(move_count == 0)
  146. {
  147. movingForward = false;
  148. movingLeft = true;
  149. movingRight = false;
  150. movingBackward = false;
  151. }
  152. ledState = HIGH;
  153. digitalWrite(RED_LED, ledState);
  154. } else {
  155. if(move_count == 0)
  156. {
  157. movingForward = true;
  158. movingLeft = false;
  159. movingRight = false;
  160. movingBackward = false;
  161. }
  162. ledState = LOW;
  163. digitalWrite(RED_LED, ledState);
  164. }
  165. }
  166. }
  167.  
  168. unsigned long moveCurrentMillis = millis();
  169. if(moveCurrentMillis - movePreviousMillis > MOVE_INTERVAL)
  170. {
  171. movePreviousMillis = moveCurrentMillis;
  172.  
  173. if((movingForward == true || movingBackward == true)||(movingLeft == true || movingRight == true))
  174. {
  175. if(move_count >= MOVE_COUNT_MAX)
  176. {
  177. line_following.all_stop();
  178. movingForward = false;
  179. movingLeft = false;
  180. movingRight = false;
  181. movingBackward = false;
  182. dont_move_count = 0;
  183. }
  184. else
  185. {
  186. move_count++;
  187. }
  188.  
  189.  
  190. }
  191. if(dont_move_count >= DONT_MOVE_COUNT_MAX)
  192. {
  193. line_following.all_stop();
  194. movingForward = false;
  195. movingLeft = false;
  196. movingRight = false;
  197. movingBackward = false;
  198. move_count = 0;
  199. }
  200. else
  201. {
  202. dont_move_count++;
  203. }
  204. }
  205.  
  206. if(movingForward == true)
  207. line_following.go_backward(robot_move_speed);
  208. if(movingLeft == true)
  209. line_following.turn_left(turn_rate);
  210. if(movingRight == true)
  211. line_following.turn_right(turn_rate);
  212. if(movingBackward == true)
  213. line_following.go_forward(robot_move_speed);
  214.  
  215.  
  216.  
  217. if (irrecv.decode(&results))
  218. {
  219. //Serial.println(results.value, HEX);
  220. switch(results.value)
  221. {
  222. case MOVE_FORWARD:
  223. movingForward = true;
  224. movingLeft = false;
  225. movingRight = false;
  226. movingBackward = false;
  227. freewill = false;
  228. // robot_move_speed = 255;
  229. break;
  230.  
  231. case MOVE_RIGHT:
  232. movingForward = false;
  233. movingLeft = false;
  234. movingRight = true;
  235. movingBackward = false;
  236. freewill = false;
  237. // robot_move_speed = 255;
  238. break;
  239.  
  240. case MOVE_LEFT:
  241. movingForward = false;
  242. movingLeft = true;
  243. movingRight = false;
  244. movingBackward = false;
  245. freewill = false;
  246. //robot_move_speed = 255;
  247. break;
  248.  
  249. case MOVE_BACKWARD:
  250. movingForward = false;
  251. movingLeft = false;
  252. movingRight = false;
  253. movingBackward = true;
  254. freewill = false;
  255. //robot_move_speed = 255;
  256. break;
  257.  
  258. case STOP_MOVE:
  259. line_following.all_stop();
  260. movingForward = false;
  261. movingLeft = false;
  262. movingRight = false;
  263. movingBackward = false;
  264. move_count = 0;
  265. freewill = false;
  266. //robot_move_speed = 255;
  267. break;
  268.  
  269. case INC_TURN_RATE:
  270. if(turn_rate < 250)
  271. {
  272. turn_rate += 50;
  273. freewill = true;
  274. //robot_move_speed = 100;
  275. }
  276. break;
  277.  
  278. case DEC_TURN_RATE:
  279. if(turn_rate > 50)
  280. turn_rate -= 50;
  281. break;
  282.  
  283. case DANCE:
  284. play_song_1();
  285. break;
  286. }
  287. //delay(200); // 1/5 second delay for arbitrary clicks. Will implement debounce later.
  288. irrecv.resume();
  289. }
  290. }
  291.  
  292. void play_song_1 ()
  293. {
  294. int size = sizeof(mario_theme_melody) / sizeof(int);
  295. for (int thisNote = 0; thisNote < size; thisNote++)
  296. {
  297. // to calculate the note duration, take one second
  298. // divided by the note type.
  299. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  300. int noteDuration = 1000/mario_theme_noteDurations[thisNote];
  301. NewTone(8, mario_theme_melody[thisNote],noteDuration);
  302.  
  303. // to distinguish the notes, set a minimum time between them.
  304. // the note's duration + 30% seems to work well:
  305. int pauseBetweenNotes = noteDuration * 1.30;
  306. delay(pauseBetweenNotes);
  307. // stop the tone playing:
  308. noNewTone(8);
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement