/** * Here we do a small LED test * (a running light with direction changing)! * For timing we use Stopwatch #1. */ void runningLight(void) { static uint8_t runLight = 1; static uint8_t dir; // Moving direction for running light // The "static" keyword in front of these two local variables means that they // are NOT cleared when leaving this function. Their content is kept and they // act just like global variables (but they are only accessible within // this function)! You already saw this on RP6 Robot Base Examples... // We want a 100ms delay between the calls, which results in 10Hz refresh rate of // the running light. // ATTENTION: DO NOT USE "==" to compare to the stopwatch values, // use only ">" or "<" or ">=" because you could miss the point where the stopwatch // has the exact value! if(getStopwatch1() > 100) // 100ms { // Set status LEDs to the value of the variable testLEDs: setLEDs(runLight); // Shift the LED bit left or right depending on direction: if(dir == 0) runLight <<= 1; else runLight >>= 1; // Change the direction if we reached one of the two outer LEDs: if(runLight > 7 ) dir = 1; else if (runLight < 2 ) dir = 0; // Reset Stopwatch1: setStopwatch1(0); }
void sendByteAndReceiveByte(void) { if(getStopwatch1() > 500) { I2CTWI_transmitByte(ARDUINO_WRITE_ADDRESS, speed); //writing the speed of rp6 uint8_t someByteToRead = 0; someByteToRead = I2CTWI_readByte(ARDUINO_READ_ADDRESS); //read the maximum speed writeInteger(someByteToRead, DEC); getSpeedLimit(someByteToRead); setStopwatch1(0); } }
/** * A small running light that uses Stopwatch 1 for timing. * The running light is updated every 100ms (= refresh frequency of 10Hz) */ void task_LEDs(void) { static uint16_t runningLight = 1; if(getStopwatch1() > 100) // have we reached AT LEAST 100ms = 0.1s? { // Update chaselight: setLEDs(runningLight); runningLight <<= 1; if(runningLight > 32) runningLight = 1; // Don't forget to reset the stopwatch to 0 again: setStopwatch1(0); } }
/** * Show a running light... */ void runningLight(void) { static uint8_t runLight = 1; static uint8_t dir; if(getStopwatch1() > 100) { setLEDs(runLight); if(dir == 0) runLight <<= 1; else runLight >>= 1; if(runLight > 7 ) dir = 1; else if (runLight < 2 ) dir = 0; setStopwatch1(0); }
int main(void) { initRP6Control(); // Always call this first! The Processor will not work // correctly otherwise. bars(2); writeString_P("\n\nRP6Control Selftest!\n\n"); bars(2); setLEDs(0b1111); mSleep(50); initLCD(); showScreenLCD("################", "################"); mSleep(400); showScreenLCD("################", "################"); showScreenLCD("RP6Control M32", "SELFTEST"); mSleep(1000); uint8_t keynumber = 0; while(keynumber < 6) { uint8_t key = checkReleasedKeyEvent(); if(key == keynumber) { keynumber++; showScreenLCD("PRESS BUTTON", "NUMBER "); writeIntegerLCD(keynumber,DEC); setLEDs(0b0000); writeString_P("### PRESS BUTTON NUMBER "); writeInteger(keynumber,DEC); writeString_P("!\n"); } } showScreenLCD("Testing", "BEEPER & LEDS"); mSleep(250); // Play a sound to indicate that our program starts: sound(50,50,100); setLEDs(0b0000); sound(80,50,100); setLEDs(0b0001); sound(100,50,100);setLEDs(0b0010); sound(120,50,100);setLEDs(0b0100); sound(140,50,100);setLEDs(0b1000); sound(160,50,100);setLEDs(0b1001); sound(180,50,100);setLEDs(0b1011); sound(200,50,100);setLEDs(0b1111); mSleep(400); setLEDs(0b0000); showScreenLCD("Testing", "EERPOM"); test(1); writeString_P("\nEEPROM TEST\n"); writeString_P("\nErasing 250 Bytes...\n"); uint8_t cnt; for(cnt = 0; cnt < 250; cnt++) { SPI_EEPROM_writeByte(cnt, 0xFF); while(SPI_EEPROM_getStatus() & SPI_EEPROM_STAT_WIP); } writeString_P("...Done!\nWriting 250 Bytes to EEPROM:\n"); for(cnt = 0; cnt < 250; cnt++) { writeIntegerLength(cnt, DEC, 3); SPI_EEPROM_writeByte(cnt, cnt); while(SPI_EEPROM_getStatus() & SPI_EEPROM_STAT_WIP); writeChar(','); if(cnt % 10 == 0) writeChar('\n'); } mSleep(400); setLEDs(0b1111); writeString_P("\nReading and verifying:\n"); for(cnt = 0; cnt < 250; cnt++) { uint8_t result = SPI_EEPROM_readByte(cnt); if(result != cnt) { writeString_P("\nEEPROM VERIFY ERROR!!!! EEPROM DAMAGED!!!\n"); writeString_P("Data read: "); writeInteger(result,DEC); writeString_P(", should be: "); writeInteger(cnt,DEC); writeChar('\n'); errors++; } else writeIntegerLength(result,DEC,3); writeChar(','); if(cnt % 10 == 0) writeChar('\n'); } writeString_P("\nErasing 250 Bytes...\n"); for(cnt = 0; cnt < 250; cnt++) { SPI_EEPROM_writeByte(cnt, 0xFF); while(SPI_EEPROM_getStatus() & SPI_EEPROM_STAT_WIP); } mSleep(400); setLEDs(0b0000); writeString_P("\nEEPROM TEST DONE!\n"); writeString_P("\nI2C TWI TEST:\n"); showScreenLCD("I2C TWI", "TEST"); I2CTWI_initMaster(100); I2CTWI_setTransmissionErrorHandler(I2C_transmissionError); uint8_t runningLight = 1; for(cnt = 0; cnt < 24; cnt++) { writeIntegerLength(cnt,DEC,3); writeChar(':'); writeIntegerLength(runningLight,DEC,3); writeChar(','); writeChar(' '); I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, 3, runningLight); I2CTWI_transmitByte(I2C_RP6_BASE_ADR, 29); uint8_t result = I2CTWI_readByte(I2C_RP6_BASE_ADR); if(result != runningLight) { writeString_P("\nTWI TEST ERROR!\n"); errors++; } runningLight <<= 1; if(runningLight > 32) runningLight = 1; if((cnt+1) % 6 == 0) writeChar('\n'); mSleep(100); } I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, 3, 0); writeString_P("\nTWI TEST DONE!\n"); writeString_P("\nMicrophone Test\n"); writeString_P("Hit the Microphone three times with your finger!\n"); showScreenLCD("MIC TEST:", ""); #define PREPARE 1 #define WAIT 2 uint8_t state = PREPARE; startStopwatch2(); while(true) { static uint8_t peak_count = 3; if(state == PREPARE) { if(getStopwatch2() > 250) { setCursorPosLCD(1, 6); writeIntegerLengthLCD( peak_count, DEC, 1); dischargePeakDetector(); state = WAIT; setStopwatch2(0); } } else if(state == WAIT) { uint8_t key = checkReleasedKeyEvent(); if(key) { break; } if(getStopwatch2() > 50) { uint16_t tmp = getMicrophonePeak(); if(tmp > 4) { externalPort.LEDS = 0; uint16_t i; uint8_t j; for(i = 0, j = 2; i < tmp; i+= 40) { if(i < 40) { externalPort.LEDS++; } else { externalPort.LEDS <<=1; externalPort.LEDS++; } } outputExt(); if(tmp > 120) { state = PREPARE; peak_count--; } if(peak_count == 0) break; } else setLEDs(0b0000); setStopwatch2(0); } } } writeString_P("\nMICROPHONE TEST DONE!\n"); showScreenLCD("ALL TESTS", "DONE!"); writeString_P("\n\n\n\n"); bars(2); writeString_P("\n\nALL TESTS DONE!\n\n"); if(errors) { bars(4); writeString_P("\nERROR ERROR ERROR ERROR ERROR ERROR ERROR\n"); writeString_P("\nATTENTION: TESTS FINISHED WITH ERRORS!!!\n"); writeString_P("PLEASE CHECK RP6-M32 ASSEMBLY!!!\n\n"); bars(4); writeString_P("\n\n"); } // Now we just show a running light... startStopwatch1(); uint8_t runLEDs = 1; uint8_t dir = 0; while(true) { if(getStopwatch1() > 100) { setLEDs(runLEDs); if(dir == 0) runLEDs <<= 1; else runLEDs >>= 1; if(runLEDs > 7 ) dir = 1; else if (runLEDs < 2 ) dir = 0; setStopwatch1(0); } }
int main(void) { initRP6Control(); // Always call this first! // The Processor will not work correctly otherwise. initLCD(); // Initialize the LC-Display (LCD) // Always call this before using the LCD! // Play two sounds with the Piezo Beeper on the RP6Control: sound(180,80,25); sound(220,80,0); //IMPORTANT: I2CTWI_initMaster(100); // Initialize the TWI Module for Master operation // with 100kHz SCL Frequency // Register the event handlers: I2CTWI_setTransmissionErrorHandler(I2C_transmissionError); // Write a text message to the UART: writeString_P("{cmd=0x"); writeInteger(CMD_LIFETIME, HEX); writeString_P("}\n"); run_testCases(); // Define a counting variable: //uint16_t counter = 0; // clear the UART buffer once at the start of the program clearReceptionBuffer(); mSleep(1000); setStopwatch1(500); startStopwatch1(); while(true) { //example of receiving some data over UART DoDataProcess(); if(move_turning!=0) { if(move_turning == 1) { rotate(60, LEFT, 3, false); } else { rotate(60, RIGHT, 3, false); } } else if(move_horizontal != 0) { if(move_horizontal > 0) { move(move_horizontal, FWD, 3, false); } else { move(move_horizontal, BWD, 3, false); } } //mSleep(100); // delay 100ms = 0.1s } return 0; }