// Arduino G-code Interpreter // v1.0 by Mike Ellery - initial software (mellery@gmail.com) // v1.1 by Zach Smith - cleaned up and did lots of tweaks (hoeken@gmail.com) // v1.2 by Chris Meighan - cleanup / G2&G3 support (cmeighan@gmail.com) // v1.3 by Zach Smith - added thermocouple support and multi-sample temp readings. (hoeken@gmail.com) // also added preliminary and experimental support for rotary encoders. #include //our command string #define COMMAND_SIZE 128 char word[COMMAND_SIZE]; byte serial_count; int no_data = 0; void setup() { //Do startup stuff here Serial.begin(19200); Serial.println("start"); Serial.println("v1.3"); //other initialization. init_process_string(); init_steppers(); init_extruder(); } void loop() { char c; //keep it hot! extruder_manage_temperature(); //read in characters if we got them. if (Serial.available() > 0) { c = Serial.read(); no_data = 0; //newlines are ends of commands. if (c != '\n') { word[serial_count] = c; serial_count++; } } //mark no data. else { no_data++; delayMicrosecondsInterruptible(100); } //if theres a pause or we got a real command, do it if (serial_count && (c == '\n' || no_data > 100)) { //process our command! process_string(word, serial_count); //clear command. init_process_string(); } //no data? turn off steppers if (no_data > 1000) disable_steppers(); } void delayMicrosecondsInterruptible(unsigned int us) { #if F_CPU >= 16000000L // for the 16 MHz clock on most Arduino boards // for a one-microsecond delay, simply return. the overhead // of the function call yields a delay of approximately 1 1/8 us. if (--us == 0) return; // the following loop takes a quarter of a microsecond (4 cycles) // per iteration, so execute it four times for each microsecond of // delay requested. us <<= 2; // account for the time taken in the preceeding commands. us -= 2; #else // for the 8 MHz internal clock on the ATmega168 // for a one- or two-microsecond delay, simply return. the overhead of // the function calls takes more than two microseconds. can't just // subtract two, since us is unsigned; we'd overflow. if (--us == 0) return; if (--us == 0) return; // the following loop takes half of a microsecond (4 cycles) // per iteration, so execute it twice for each microsecond of // delay requested. us <<= 1; // partially compensate for the time taken by the preceeding commands. // we can't subtract any more than this or we'd overflow w/ small delays. us--; #endif // busy wait __asm__ __volatile__ ( "1: sbiw %0,1" "\n\t" // 2 cycles "brne 1b" : "=w" (us) : "0" (us) // 2 cycles ); }