Guest User

Untitled

a guest
Jun 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. //#include <avr/io.h>
  2. #include <avr/wdt.h> // for watchdog timer function
  3. //#include <PololuQTRSensors.h> // for line sensor
  4.  
  5. //#define NUM_SENSORS   8     // number of sensors used
  6. //#define TIMEOUT       2500  // waits for 2500 us for sensor outputs to go low
  7.  
  8.  
  9. //function prototypes make them return values later
  10. void readrange(); // range sensor
  11. //void accel(); // accelerometer
  12. //void line(); // line sensor
  13. void motorforward(); // motor forward control
  14.  
  15.   // global variables can later be moved into a class header file
  16.   const int xPin = 9; // x pin for accelerometer
  17.   const int yPin = 10; // y pin for accelerometer
  18.   int analogInPin = A0; // pin for range sensor
  19.   int m1count; // count for motor 1
  20.   int m2count;
  21.   int m1_1 = 2; // motor 1 pin 2
  22.   int m1_2 = 3; // motor 1 pin 3
  23.   int m1_3 = 4; // motor 1 pin 4
  24.   int m1_4 = 5; // motor 1 pin 5
  25.  
  26.   int m2_1 = 6; // motor 2 pin 6
  27.   int m2_2 = 7; // motor 2 pin 7
  28.   int m2_3 = 8; // motor 2 pin 8
  29.   int m2_4 = 9; // motor 2 pin 9
  30.  
  31.   // (line sensor instance variable) Sensors connected to pins 5 to 12
  32. //PololuQTRSensorsRC qtrrc((unsigned char[]) {3,4,5,6,7,8,9,10},  NUM_SENSORS,
  33. //                          TIMEOUT, QTR_NO_EMITTER_PIN);
  34. //unsigned int sensorValues[NUM_SENSORS];
  35.  
  36. void setup() { // setup pins
  37.  
  38.   //wdt_enable(WDTO_2S); // threshold interval of 2 seconds for watchdog timer (can go up to 8 seconds)
  39.   // if no signal for more than 2 seconds, reboot MCU
  40.   // wdt_reset();
  41.  
  42.   Serial.begin(9600); //9600 baud rate
  43.  
  44.   pinMode(analogInPin, INPUT); // range sensor analog input 0
  45.   pinMode(xPin, INPUT); // accelerometer x digital input 9
  46.   pinMode(yPin, INPUT); // accelerometer y digital input 10
  47.  
  48.   pinMode(m1_1, OUTPUT);
  49.   pinMode(m1_2, OUTPUT);
  50.   pinMode(m1_3, OUTPUT);
  51.   pinMode(m1_4, OUTPUT);
  52.  
  53.   pinMode(m2_1, OUTPUT);
  54.   pinMode(m2_2, OUTPUT);
  55.   pinMode(m2_3, OUTPUT);
  56.   pinMode(m2_4, OUTPUT);
  57.  
  58. }
  59.  
  60. void loop() { // call functions
  61.  
  62. motorforward();
  63. //readrange(); // range sensor function
  64. //accel(); // accelerometer function
  65. //line(); // line sensor function
  66. }
  67.  
  68. void readrange(){  // range sensor function
  69.  
  70.   float sensorValue; // value of sensor      
  71.   int temp; // temporary variable for storing value
  72.  
  73.   temp = analogRead(analogInPin);
  74.   sensorValue = (6787.0 /((float)temp - 3.0)) - 4.0; // conversion to cm
  75.  // approximately 10 to 80 cm
  76.  
  77. if(sensorValue <= 80 && sensorValue >= 0){
  78. Serial.println(sensorValue);
  79. }
  80. else {
  81.  Serial.println("out of range");
  82. }
  83.  
  84. delay(300);
  85.  // pass the sensorValue variable to the motor function
  86.  
  87. }
  88.  
  89. void accel(){  // accelerometer function
  90.   int pulseX;
  91.   int pulseY;
  92.   int accelerationX; // in milli-g's
  93.   int accelerationY; // in milli-g's
  94.  
  95.   pulseX = pulseIn(xPin,HIGH);  // pulseIn function from library
  96.   pulseY = pulseIn(yPin,HIGH);
  97.   // convert the pulse width into acceleration
  98.   // earth gravity (1000 milli-g's)
  99.   // 1/1000 of earth's gravity
  100.   accelerationX = ((pulseX / 10) - 500) * 8;
  101.   accelerationY = ((pulseY / 10) - 500) * 8;
  102.  
  103.   // formatting and printing
  104.   Serial.print("X=");
  105.   Serial.print(accelerationX);
  106.   Serial.print("\t Y=");
  107.   Serial.print(accelerationY);
  108.   Serial.println();
  109.  
  110.   delay(300);
  111. }
  112.  
  113. //void line(){ // line sensor function
  114.  
  115. // qtrrc.read(sensorValues);
  116.  
  117.   // print the sensor values
  118. //  unsigned char i;
  119. //  for (i = 0; i < NUM_SENSORS; i++)
  120. //  {
  121. //    Serial.print(sensorValues[i]);
  122. //    Serial.print("   ");
  123. //  }
  124. //  Serial.println(" ");
  125. //  delay(250);
  126.  
  127. //}
  128.  
  129. // motor forward function
  130. void motorforward(){
  131.  
  132.   digitalWrite(m1_1, HIGH);
  133.   digitalWrite(m1_2, LOW);
  134.   digitalWrite(m1_3, LOW);
  135.   digitalWrite(m1_4, LOW);
  136.   delay(20);
  137.   digitalWrite(m1_1, LOW);
  138.   digitalWrite(m1_2, HIGH);
  139.   digitalWrite(m1_3, LOW);
  140.   digitalWrite(m1_4, LOW);
  141.   delay(20);
  142.   digitalWrite(m1_1, LOW);
  143.   digitalWrite(m1_2, LOW);
  144.   digitalWrite(m1_3, HIGH);
  145.   digitalWrite(m1_4, LOW);
  146.   delay(20);
  147.   digitalWrite(m1_1, LOW);
  148.   digitalWrite(m1_2, LOW);
  149.   digitalWrite(m1_3, LOW);
  150.   digitalWrite(m1_4, HIGH);
  151.   delay(20);
  152.  
  153.   digitalWrite(m2_1, HIGH);
  154.   digitalWrite(m2_2, LOW);
  155.   digitalWrite(m2_3, LOW);
  156.   digitalWrite(m2_4, LOW);
  157.   delay(20);
  158.   digitalWrite(m2_1, LOW);
  159.   digitalWrite(m2_2, HIGH);
  160.   digitalWrite(m2_3, LOW);
  161.   digitalWrite(m2_4, LOW);
  162.   delay(20);
  163.   digitalWrite(m2_1, LOW);
  164.   digitalWrite(m2_2, LOW);
  165.   digitalWrite(m2_3, HIGH);
  166.   digitalWrite(m2_4, LOW);
  167.   delay(20);
  168.   digitalWrite(m2_1, LOW);
  169.   digitalWrite(m2_2, LOW);
  170.   digitalWrite(m2_3, LOW);
  171.   digitalWrite(m2_4, HIGH);
  172.   delay(20);
  173.   }
Add Comment
Please, Sign In to add comment