http://www.reprap.org/wiki/Clone_Wars:_Papa_quiero_un_robot:_CachaTuga
La idea es controlarla con scratch (S4A) y a distancia con un módulo BT.
//**************************************************************** /* * Water level test (based in Watchdog Sleep) * Probes on analog0 and pin 10 * Indicator LED in pin 13 */ //**************************************************************** #include <avr/sleep.h> #include <avr/wdt.h> #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif #ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif int nint; // interaction number int ntimes=255; // the longest time whatchdog can manage is 9= 8sec but it is enough to read the level each 30 minutes. So here we put 225 (=30x60/8) int pinLed=13; // int pinFeeder=10; volatile boolean f_wdt=1; void setup(){ Serial.begin(9600); pinMode(pinLed,OUTPUT); pinMode(pinFeeder,OUTPUT); Serial.println("Water level test"); // CPU Sleep Modes // SM2 SM1 SM0 Sleep Mode // 0 0 0 Idle // 0 0 1 ADC Noise Reduction // 0 1 0 Power-down // 0 1 1 Power-save // 1 0 0 Reserved // 1 0 1 Reserved // 1 1 0 Standby(1) cbi( SMCR,SE ); // sleep enable, power down mode cbi( SMCR,SM0 ); // power down mode sbi( SMCR,SM1 ); // power down mode cbi( SMCR,SM2 ); // power down mode setup_watchdog(9); // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec } byte del; int cnt; byte state=0; int water=0; //**************************************************************** //**************************************************************** //**************************************************************** void loop(){ int modulo=0; if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs f_wdt=0; // reset flag // Read only each ntimes times modulo=nint%ntimes; if (modulo==0){ digitalWrite(pinFeeder,1); // send voltage in the water delay(10); water=analogRead(0); // reading probes digitalWrite(pinFeeder,0); // stop voltage in the water Serial.print("water: " ); Serial.println(water ); switch (state){ case 0: // probes are in the water if (water < 900) { // if there is no currente between the probes state=1; digitalWrite(pinLed,1); // switch on the warning light } break; case 1: // probes out of the water if (water > 1000) { // if some current go through the probes state=0; digitalWrite(pinLed,0); // switch off the warning light } break; } } nint++; Serial.print("Sleep " ); Serial.println(nint ); delay(2); // wait until the last serial character is send // pinMode(pinLed,INPUT); // set all used port to intput to save power pinMode(pinFeeder,INPUT); // set feeder port to intput to save power system_sleep(); // pinMode(pinLed,OUTPUT); // set all ports into state before sleep pinMode(pinFeeder,OUTPUT); // set feeder port into state before sleep } } //**************************************************************** // set system into the sleep state // system wakes up when wtchdog is timed out void system_sleep() { cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here sleep_enable(); sleep_mode(); // System sleeps here sleep_disable(); // System continues execution here when watchdog timed out sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON } //**************************************************************** // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec void setup_watchdog(int ii) { byte bb; int ww; if (ii > 9 ) ii=9; bb=ii & 7; if (ii > 7) bb|= (1<<5); bb|= (1<<WDCE); ww=bb; Serial.println(ww); MCUSR &= ~(1<<WDRF); // start timed sequence WDTCSR |= (1<<WDCE) | (1<<WDE); // set new watchdog timeout value WDTCSR = bb; WDTCSR |= _BV(WDIE); } //**************************************************************** // Watchdog Interrupt Service / is executed when watchdog timed out ISR(WDT_vect) { f_wdt=1; // set global flag }
Tamaño inicial Raspberry Pi |
Tamaño de producción... como una tarjeta de crédito. |
/ An interactive bear for Charlie -- RFID triggered sound responses #include <FatReader.h> #include <SdReader.h> #include "WaveHC.h" #include "WaveUtil.h" SdReader memcard; FatVolume vol; FatReader root; FatReader file; WaveHC wave; #define ENABLE 7 // Set the pin number for enabling the RFID reader. The Audio Shield uses pins 2-5. #define STOPTAG "3D001EDA94" // Code of the TAG that stops any sound int val = 0; char code[10]; int bytesread = 0; char filename[1][13]; void setup() { //////// Set up RFID reader to collect tag information ///////////////////// Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps pinMode(ENABLE,OUTPUT); // Set digital pin 7 as OUTPUT to connect it to the RFID /ENABLE pin digitalWrite(ENABLE, LOW); // Activate the RFID reader //////// Set the pins to output for driving the Audio Shield pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); ////////// Set up the Audio Shield by initializing the memory card for reading //////// if (!memcard.init()) { // If something went wrong sdErrorCheck prints out the error check putstring_nl("Card init. failed!"); cardErrorCheck(); return; } //This will optimize the reading of the memory card -- remove it if it times out memcard.partialBlockRead(true); // Find a FAT formatted partition by looking in teh first five slots. Remember your memory card should be FAT16 or FAT32 formatted uint8_t partition; for (partition = 0; partition < 5; partition++) { if (vol.init(memcard, partition)) break; } if (partition == 5) { putstring_nl("No valid FAT partition"); cardErrorCheck(); while(1); // This is a point of no return. Format your memory card properly and try again. } // Open the root directory for reading the files if (!root.openRoot(vol)) { putstring_nl("Can't open root directory"); while(1); // Something went wrong here so investigate the file system on your memory card. } // If you got this far then the card is ready to read /*Serial.println("STOP TAG is : "); Serial.println(STOPTAG); putstring_nl("Ready to go");*/ } // If we find an error, check what the error is and show it on the serial terminal void cardErrorCheck(void) { if(!memcard.errorCode()) return; putstring("\n\rSD I/O error:"); Serial.print(memcard.errorCode()); putstring(", "); Serial.print(memcard.errorData()); while(1); // Stick here if there is an error } void loop() { if(Serial.available() > 0) { // if data available from reader if((val = Serial.read()) == 10) { // check for header bytesread = 0; while(bytesread<10) { // read 10 digit code if( Serial.available() > 0) { val = Serial.read(); if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading break; // stop reading } code[bytesread] = val; // add the digit bytesread++; // ready to read next digit } } if(bytesread == 10) { // if 10 digit read is complete if(memcmp(code,STOPTAG,10) == 0 ){ // check if the TAG is the STOP TAG silence(); //Serial.println("STOP TAG "); } else if (! wave.isplaying){ // do nothing if a file is alredy playing playsound(code); // Serial.print("TAG code is: "); // possibly a good TAG // Serial.println(code); // print the TAG code Serial.flush(); // Flush the serial buffer before trying to read a new code } } bytesread = 0; } } } void playsound(char codetoplay[]) { Serial.print("File name is: "); // possibly a good TAG //for(int i = 2; i<10; i++) { // Make a filename from the first 8 characters of the RFID tag number NOT WORKING for(int i = 0; i<8; i++) { filename[0][i]=codetoplay[i]; //Serial.print(codetoplay[i]); // print the name code } filename[0][8]='.'; filename[0][9]='w'; filename[0][10]='a'; filename[0][11]='v'; //Serial.println(".wav"); // print the name code silence(); //shut down anything that is currently playing and close that file playfile(filename[0]); } void playfile(char *name) { if (!file.open(root, name)) { putstring_nl("Couldn't open file"); return; } if (!wave.create(file)) { putstring_nl("Not a valid WAV"); return; } wave.play(); } void silence() { if(wave.isplaying) { wave.stop(); } }