Guest User

Untitled

a guest
Jun 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1.  
  2. #include <TimedAction.h>
  3. #include <avr/wdt.h> // for watchdog timer function
  4.  
  5. TimedAction forwardAction = TimedAction(100,moveForward);
  6. TimedAction rangeAction = TimedAction(500,readrange);
  7.  
  8. void readrange(); // range sensor
  9. void accel(); // accelerometer
  10.  
  11. int motorPins[] = {2, 3, 4, 5};
  12. int motorPins2[] = {6, 7, 8, 9};
  13. int count = 0;
  14. int count2 = 0;
  15. int delayTime = 4;
  16. int val = 0;
  17. int analogInPin = A0; // pin for range sensor
  18. const int xPin = 9; // x pin for accelerometer
  19. const int yPin = 10; // y pin for accelerometer
  20. float sensorValue; // value of sensor    
  21.  
  22.  
  23. void setup() {
  24.   Serial.begin(9600); //9600 baud rate
  25.  
  26.   for (count = 0; count < 4; count++) {
  27.     pinMode(motorPins[count], OUTPUT);
  28.   }
  29.  
  30.   pinMode(analogInPin, INPUT); // range sensor analog input 0
  31.   pinMode(xPin, INPUT); // accelerometer x digital input 9
  32.   pinMode(yPin, INPUT); // accelerometer y digital input 10
  33.  
  34. }
  35.  
  36.  
  37. void moveBackward() {
  38.  
  39.   if ((count2 == 0) || (count2 == 1)) {
  40.     count2 = 16;
  41.   }
  42.   count2>>=1;
  43.   for (count = 3; count >= 0; count--) {
  44.     digitalWrite(motorPins[3 - count], count2>>count&0x01);
  45.   }
  46.   delay(delayTime);
  47.  
  48.  
  49. }
  50.  
  51. void readrange(){  // range sensor function
  52.  //forwardAction.disable();
  53.      
  54.   int temp; // temporary variable for storing value
  55.  
  56.   temp = analogRead(analogInPin);
  57.   sensorValue = (6787.0 /((float)temp - 3.0)) - 4.0; // conversion to cm
  58.  // approximately 10 to 80 cm
  59.  
  60. if(sensorValue <= 80 && sensorValue >= 0){
  61. Serial.println(sensorValue);
  62. }
  63. else {
  64.  Serial.println("out of range");
  65. }
  66.  
  67. delay(300);
  68.  // pass the sensorValue variable to the motor function
  69. //forwardAction.enable();
  70. }
  71.  
  72. void moveForward() {
  73.  
  74.   while (sensorValue >= 20){
  75.   if ((count2 == 0) || (count2 == 1)) {
  76.     count2 = 16;
  77.   }
  78.   count2>>=1;
  79.   for (count = 3; count >= 0; count--) {
  80.     digitalWrite(motorPins[count], count2>>count&0x01);
  81.   }
  82.   delay(delayTime);
  83.  
  84.   }
  85. }
  86.  
  87. void loop() {
  88.     val = 1000;
  89.     //val = 400;
  90.     //moveForward();
  91.     //readrange(); // range sensor function
  92.     //accel(); // accelerometer function
  93.    
  94.     forwardAction.check();
  95.     rangeAction.check();
  96.    
  97.     if (val < 480) {
  98.     moveBackward();
  99.  
  100. }
  101. }
Add Comment
Please, Sign In to add comment