// Detects an incoming RFID. Returns 1 if a known ID is detected, 0 otherwise. int Skyscraper::readRFID() { Serial.println("Tower "+String(_id)+":"); String rfid = ""; // stores RFID string char val = 0; // stores incoming character int counter = 0; // breaks loop if no RFID // reset readers for (int i = 3; i < 8; i++) { if (i == _rfidPin) { // Serial.println(" digitalWrite("+String(i)+", HIGH);"); digitalWrite(i, HIGH); } else { // Serial.println(" digitalWrite("+String(i)+", LOW);"); digitalWrite(i, LOW); } } delay(300); // read the serial port while (Serial.available()) { if (counter > 20 || rfid.length() == 12) { break; } else { val = Serial.read(); if (isalnum(val)) rfid.concat(val); counter++; } } Serial.println(" rfid:"+rfid); // check color for (int i = 0; i < NUM_RINGS; i++) { if (rfid == clean(redID[i])) { Serial.println(" detected red!"); if (_color != "red") { analogWrite(_red, 255); analogWrite(_blue, 0); _color = "red"; } turnServo(); return 1; } else if (rfid == clean(blueID[i])) { Serial.println(" detected blue!"); if (_color != "blue") { analogWrite(_red, 0); analogWrite(_blue, 255); _color = "blue"; } turnServo(); return 1; } } return 0; // no rfid detected }
/* -Pre-Reqs: SetupPWM(uint32_t prescale) with prescale = 10 setupI2C1() setupUART() setupADC() -Globals: sweepspeed samplespersweep sweepstartpos sweepstoppos -Desc: Moves servo between servo start and end position at a set speed, scans a set number of times. If '0' is pressed on the keypad, quits. */ void scanMode(void){ initServo(1); setServo(sweepstartpos); SystickTimer(100); // gap between scans = scan range / sample speed uint32_t scangap = (uint32_t)((float)(sweepstoppos - sweepstartpos)/(float)samplespersweep); char button; int turnAmt; int dir = 1; int i = samplespersweep - 1; while(1){ // If servo at edge, change direction if(servoAtEdge()){ dir = dir*-1; startSweep(); i = samplespersweep - 1; averageDistance = _averageSweep(); } // Turn servo in the "dir" direction by "sweepspeed" turnAmt = dir*sweepspeed; turnServo(turnAmt); if(servo_position % scangap < sweepspeed){ currentIr = irMedian(); sendScanData(currentIr); sweep[i] = currentIr; currentRawIr = irMedianRaw(); } // If 0 was pressed on the keypad, break out of the loop. button = KeypadTest(); if(button == '0'){ break; }; SystickTimer(10); } }