/** * Board: Pro Mini 5V * * Chained 74HC595 8 bit shift register demo. Knight Rider! * * FIXME: add in to activate launch. * FIXME: make it work on a blueSMIRTH bluetooth modem connected to blueterm on android. * FIXME: delay der sequence soll nach dem pin liegen anstatt vor. delay * anstatt wait * FIXME: programm description * * $Id: fireworks.pde 402 2011-12-23 16:42:54Z wunderlins $ */// include library (must be installed unter <sketches>/libraries/MM74HC595/)// http://spliffy.freeshell.net/hardware/MM74HC595.zip#include <EEPROM.h>#include <avr/pgmspace.h>#include <MM74HC595.h>/** * 74HC595 8 bit shift register configration * * PIN_SER: is the arduino serial pin * PIN_SCK: is the arduino pin for the shift register * PIN_RCK: is the arduino pin for the output register */#define PIN_SER 7 // pin 14 on the 74HC595#define PIN_SCK 9 // pin 12 on the 74HC595#define PIN_RCK 8 // pin 11 on the 74HC595/** * IRLZ44N MOSFET pins * * From left to right (cooling mount on the backside) * 1 Gate * 2 Drain * 3 Source * */// input buffer#define WORD_LENGTH 10#define NUM_WORDS 10#define NEWLINE "\n"#define DEBUG 0// command settings#define NUMBER_OF_PINS 24#define NUMBER_OF_BYTES 2#define SEQUENCE_START 128#define PIN_NULL 255 // pin value 255 is reserved as undefined/NULL// exit codes#define EXIT_SUCCESS 0#define EXIT_UNKNOWN_COMMAND 1#define EXIT_ARGS 2#define EXIT_POS_TOO_LONG 3#define EXIT_PIN_TOO_LONG 4#define EXIT_DELAY_TOO_LONG 5#define EXIT_UNKNOWN_CFG_MODE 6#define EXIT_CFG_TOO_FEW_ARGUMENTS_SET 7#define EXIT_CFG_TOO_FEW_ARGUMENTS_GET 8#define EXIT_NUM_TOO_LONG 9#define EXIT_BAUDE_TOO_LONG 10#define EXIT_CFG_MODE_UNKNOWN_GET 11#define EXIT_CFG_MODE_UNKNOWN_SET 12#define EXIT_BAUDE_UNKNOWN 13#define EXIT_POS_TOO_SHORT 14#define EXIT_UNKNOWN 127// eeprom settings#define CONFIG_POS_NUM 0#define CONFIG_POS_BAUDE 1#define CONFIG_POS_DELAY 2#define NUM_BAUDE_RATES 8// globalsint cc, wc = 0;char words[NUM_WORDS][WORD_LENGTH];uint8_t num = 0;uint8_t regs = 0;uint8_t baude = 0;uint8_t stayon = 0;uint32_t baude_rates[NUM_BAUDE_RATES] = { 0, 2400, 4800, 9600, 19200, 38400, 57600, 115200};#if WITH_MOSFET_TEST == truebool mosfet = false;int pin_mosfet = 9;#endif// default eeprom valuesuint8_t default_num = 8; // default 8uint8_t default_baude = 3; // default 9600uint8_t default_delay = 10; // default 1.0 secsconst PROGMEM prog_uchar help_text[] = { "Commands:" NEWLINE " r reset" NEWLINE " x reset sequences" NEWLINE " d|p print configuration" NEWLINE " s sequence" NEWLINE " <uint8_t> pos, <uint8_t> pin, <uint8_t> delay - store a sequence in EEPROM" NEWLINE " c system configuration" NEWLINE " c set <char[]> keyword <uint8_t> value" NEWLINE " c get <char[]> keyword" NEWLINE " t selftest (CAUTION: do nut run when armed!)" NEWLINE " l launch" NEWLINE " h this help text" NEWLINE NEWLINE " Keywords" NEWLINE " num - number of rockets" NEWLINE " baude - baude rate" NEWLINE " delay - 10/sec for how long a pin should be on" NEWLINE NEWLINE};void setup() { // init mosfet test pin #if WITH_MOSFET_TEST == true pinMode(pin_mosfet, OUTPUT); digitalWrite(pin_mosfet, LOW); #endif /* // Reset eeprom EEPROM.write(CONFIG_POS_NUM, default_num); EEPROM.write(CONFIG_POS_BAUDE, default_baude); EEPROM.write(CONFIG_POS_DELAY, default_delay); delay(200); */ baude = EEPROM.read(CONFIG_POS_BAUDE); // setup number of rockets from eeprom. EEPROM(0) stores numer of rockets, // each register can control 8 rockets. num = EEPROM.read(CONFIG_POS_NUM); regs = (uint8_t)ceil(num/8.0); stayon = EEPROM.read(CONFIG_POS_DELAY); Serial.begin(baude_rates[baude]); delay(20); Serial.print("Initializing "); Serial.print(num, DEC); Serial.print(" rockets, "); Serial.print(regs, DEC); Serial.print(" shift registers, speed "); Serial.print(baude_rates[baude]); Serial.print(", delay "); Serial.print(stayon, DEC); Serial.print(" ... "); // shift register instance MM74HC595 registers(PIN_SER, PIN_SCK, PIN_RCK, regs); delay(20); registers.reset(); registers.update(); Serial.println("done");}/** * reset word buffer and counters */void reset_input_buffer() { for(int i=0; i<NUM_WORDS; i++) words[i][0] = '\0'; wc = 0; cc = 0;}void loop() { // no serial data, pass if there is no command to be processed. if (!Serial.available()) { return; } delay(20); // FIXME: does this work on 8MHz? // read serial data while(Serial.available()) { char c = Serial.read(); if (c == '\n' || c == '\r' || c == '\0') { words[wc][cc] = '\0'; wc++; break; } if (c == ' ') { words[wc][cc] = '\0'; wc++; cc = 0; if (wc == NUM_WORDS) { Serial.flush(); delay(20); break; } continue; } words[wc][cc++] = c; // prevent out of bound if (cc == WORD_LENGTH) { wc++; words[wc][cc+1] = '\0'; Serial.flush(); delay(20); break; } } // check if we have serial input if(wc > 0) { // interpret and run command int ret = run_command(wc, words); #if DEBUG Serial.print("Returned: "); #endif number_pad(ret, 3, '0'); Serial.print("\n"); } // reset word buffer and counters reset_input_buffer();}/** * convert number to string, right pad with padding character */void number_pad(int number, int length, char padding_char) { int i = number; int int_l = 0; while(i = (i/10) > 0) int_l++; while(--length > int_l) Serial.print(padding_char); Serial.print(number, DEC);}/*void loop(){ // reset all pins (set to LOW) registers.reset(); // set pin(s) to high registers.set(i, true); // activate outputs registers.update(); // check if we have to change direction if (i+1 == NUM_SHIFT_REGISTERS * 8) { direction = -1; } else if (i == 0) { direction = 1; } i += direction; delay(STEP_DELAY);}*/