Hey Guys
Im trying to serial print data collected from the QTR-8RC with a timestamp. I have gotten it to work and I can serial print every 25ms. Once I got that working I tried to implement my code for the motors so they simply pulse a set PWM signal for a set time and then reverse it so the result is the robot will just keep moving back and fourth over the line. Its a line follower by the way. When I put both pieces of code together it does as expected and runs the loop but then serial prints the line position and time stamp after its finished pulsing the motor one way and then back the other way, so every 2 seconds. My problem is I want to keep recording data every 25ms whilst simultaneously pulsing the motors back and fourth. Thanks for your help in advanced and hope I have explained my problem well. I know the example code Blink Without Delay touches on what I want to do but I cant seem to wrap my head around it.
// MOTOR MOTOR MOTOR MOTOR
int E1 = 10; // RIGHT MOTOR PWM
int M1 = 12; // RIGHT MOTOR DIRECTION
int E2 = 11; // LEFT MOTOR PWM
int M2 = 13; // LEFT MOTOR DIRECTION
//TIMER TIMER TIMER TIMER
unsigned long time;
//SENSOR SENSOR SENSOR SENSOR
#include <QTRSensors.h>
#define NUM_SENSORS 8 // Number of Sensors Used
#define TIMEOUT 2500 // Waits for 2500 Microseconds for Sensor Outputs to go Low
#define EMITTER_PIN QTR_NO_EMITTER_PIN // Eemitter is Controlled by Digital Pin 2, No Emitter Pin
QTRSensorsRC qtrrc((unsigned char[]) {2, 3, 4, 5, 6, 7, 8, 9}, NUM_SENSORS, TIMEOUT, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
void setup()
{
//MOTOR MOTOR MOTOR MOTOR
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
// TIMER TIMER TIMER TIMER
Serial.begin(9600);
// SENSOR SENSOR SENSOR SENOR
for (int i = 0; i < 200; i++) // Calibration Time
{
qtrrc.calibrate(); // Reads All Sensors 10 Times at 2500 us Per Read (i.e. ~25 ms per call)
}
delay(1000);
}
void loop()
{
//MOTOR MOTOR MOTOR MOTOR
// TURN RIGHT
digitalWrite(M1, LOW); //RIGHT MOTOR REVERSE
digitalWrite(M2, HIGH); //LEFT MOTOR FORWARD
analogWrite(E1, 150); //RIGHT MOTOR PWM
analogWrite(E2, 150); //LEFT MOTOR PWM
delay(50); //TIME
// STOP
digitalWrite(M1, LOW); //STOP
digitalWrite(M2, LOW); //STOP
analogWrite(E1, 0); //STOP
analogWrite(E2, 0); //STOP
delay(1000);
// TURN LEFT
digitalWrite(M1, HIGH); //RIGHT MOTOR FORWARD
digitalWrite(M2, LOW); //LEFT MOTOR REVERSE
analogWrite(E1, 150); //RIGHT MOTOR PWM
analogWrite(E2, 150); //LEFT MOTOR PWM
delay(50); //TIME
// STOP
digitalWrite(M1, LOW); //STOP
digitalWrite(M2, LOW); //STOP
analogWrite(E1, 0); //STOP
analogWrite(E2, 0); //STOP
delay(1000);
// TIMER TIMER TIMER TIMER
Serial.print("Time: ");
time = millis();
Serial.println(time); //Prints Time Since Program Started
delay(25); // Time Stamp Sample Rate
// SENSOR SENSOR SENSOR SENSOR
unsigned int position = qtrrc.readLine(sensorValues); // Read Calibrated Sensor Values and Obtain a Measure of the Line Position from 0 to 7000
Serial.println(position); // Prints Line Positions Values from 0-7000
}