User:JBers/enes100/My Work3/turnFanOnAndOff

//Code for the QRE1113 Digital board //Outputs via the serial terminal - Lower numbers mean more reflected //3000 or more means nothing was reflected.

int infrared = 2; //connected to digital 2

void setup{ Serial.begin(9600); //establish motor direction toggle pins pinMode(12, OUTPUT); //drive motor -- HIGH = forwards and LOW = backwards //Sets initial direction of drive motor digitalWrite(12, HIGH); }

void loop{ int QRE_Value = readQD; Serial.println(QRE_Value); }

int readQD{ //Returns value from the QRE1113 //Lower numbers mean more refleacive //More than 3000 means nothing was reflected. pinMode( infrared, OUTPUT ); digitalWrite( infrared, HIGH ); delayMicroseconds(10); pinMode( infrared, INPUT );

long time = micros; //time how long the input is HIGH, but quit after 3ms as nothing happens after that while (digitalRead == HIGH && micros - time < 3000); int diff = micros - time; if (diff < 300){ //set drive motor backwards direction digitalWrite(12, HIGH); } else{ //   //Setting drive motor //   //set drive motor forward direction digitalWrite(12, LOW); } return diff; }