//------------------------------------------------------------------------------ // 2012-05-01 dan@marginallyclever.com // Sample code for introduction to arduino night //------------------------------------------------------------------------------ // includes bring code into your project that doesn't have to be in this file. // It's like buying a tool for your workshop instead of making it from scratch. #include // Defines are constants in a program. #define BAUD (9600) #define LISTEN_LEN (64) // Globals are data available from everywhere. char buffer[LISTEN_LEN]; Servo s1; long time_start; long time_now; long time_accumulated; long dt; //------------------------------------------------------------------------------ // advance our clock counter void ticktock() { long this_instant = millis(); dt = this_instant - time_now; time_now = this_instant; time_accumulated += dt; } //------------------------------------------------------------------------------ // print something helpful. void help() { Serial.println("HELP; (prints this message)"); Serial.println("SET Xn.nn Yn.nn; (sets X and Y to a decimal value)"); } //------------------------------------------------------------------------------ // process a single command from the user void processCommand() { if( !strncmp(buffer,"HELP",4) ) { help(); } else if( !strncmp(buffer,"SET",8) ) { double xx = 0; double yy = 0; char *ptr=buffer; while(ptr && ptr 0) { // read in a character buffer[sofar++]=Serial.read(); // if we hit a semi-colon, assume end of instruction. // there may be more in the buffer but we'll get it later. if(buffer[sofar-1]==';') break; } // Only process instructions when they are properly terminated. // Punctuation is important! if(sofar>0 && buffer[sofar-1]==';') { // End the line so string functions work buffer[sofar]=0; // Do something with the command processCommand(); // Reset the listen buffer sofar=0; // Ready for instructions Serial.print("> "); } } //------------------------------------------------------------------------------ // Start the arduino board void setup() { // Start communications at speed BAUD Serial.begin(BAUD); // Send our first message Serial.println("== HELLO WORLD =="); // Reset the listen buffer sofar=0; // Start the clock dt=time_accumulated=0; time_start=time_now=millis(); help(); // Ready for instructions Serial.print("> "); } //------------------------------------------------------------------------------ // The main loop void loop() { ticktock(); dealWithUserCommands(); // do other stuff here Serial.print(time_now); Serial.println("\t"); Serial.print(dt); Serial.println("\t"); Serial.print(time_accumulated); Serial.println("\t"); Serial.print(time_now-time_start); Serial.println("\t"); delay(250); // 250ms = 0.25 second }