Hello, im trying to make a bike computer for my bike, but i would also like it to program my blinkm in the same sketch. I made this, but now it just doesnt work at all (it runs, but doesnt program the LED, and i havent tested counting yet)
#include "BlinkM_funcs.h"
#include "Wire.h"
#define BLINKM_ARDUINO_POWERED 1
#define CMD_START_BYTE 0x01
int blinkm_addr = 0x09;
byte serInStr[32]; // array that will hold the serial input string
int ledPin = 13;
//-----------------------------------------------
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
void setup()
{
Serial.begin(9600);
digitalWrite(2, HIGH);
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
//-----------------------------------------------
pinMode(ledPin, OUTPUT);
if( BLINKM_ARDUINO_POWERED ) {
BlinkM_beginWithPower();
} else {
BlinkM_begin();
}
// FIXME:
BlinkM_setAddress( blinkm_addr ); // comment out to not set address
Serial.begin(19200);
byte rc = BlinkM_checkAddress( blinkm_addr );
if( rc == -1 )
Serial.println("no response");
else if( rc == 1 )
Serial.println("addr mismatch");
Serial.println("BlinkMCommander ready");
#ifdef DEBUG
Serial.println("DEBUG MODE");
#endif
}
void loop()
{
if (rpmcount >= 20) {
//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm,DEC);
int num;
//read the serial port and create a string out of what you read
num = readCommand(serInStr);
if( num == 0 ) // see if we got a proper command string yet
return;
digitalWrite(ledPin,HIGH); // say we're working on it
byte addr = serInStr[1];
byte sendlen = serInStr[2];
byte recvlen = serInStr[3];
byte* cmd = serInStr+4;
#ifdef DEBUG
Serial.print(" addr:"); Serial.print(addr,HEX);
Serial.print(" sendlen:"); Serial.print(sendlen,HEX);
Serial.print(" recvlen:"); Serial.print(recvlen,HEX);
Serial.print(" cmd[0..7]:"); Serial.print(cmd[0],HEX);
Serial.print(","); Serial.print(cmd[1],HEX);
Serial.print(","); Serial.print(cmd[2],HEX);
Serial.print(","); Serial.print(cmd[3],HEX);
Serial.print(","); Serial.print(cmd[4],HEX);
Serial.print(","); Serial.print(cmd[5],HEX);
Serial.print(","); Serial.print(cmd[6],HEX);
Serial.print(","); Serial.println(cmd[7],HEX);
#endif
BlinkM_sendCmd(addr, cmd, sendlen);
// if looking for a response, get it
if( recvlen!=0 ) {
byte resp[16];
int rc = BlinkM_receiveBytes(addr, resp, recvlen);
for( int i=0; i<recvlen; i++)
Serial.print(resp[i],BYTE);
}
for(int i=0; i<30;i++)
serInStr[i] = 0; // say we've used the string (not needed really)
digitalWrite(ledPin,LOW); // show we're done
}
}
void rpm_fun()
{
rpmcount++;
//Each rotation, this interrupt function is run twice
}
//-----------------------------------------------
//read a string from the serial and store it in an array
//you must supply the str array variable
//returns number of bytes read, or zero if fail
uint8_t readCommand(byte *str)
{
uint8_t b,i;
if( ! Serial.available() ) return 0; // wait for serial
b = Serial.read();
if( b != CMD_START_BYTE ) // check to see we're at the start
return 0;
#ifdef DEBUG
Serial.println("startbyte");
#endif
str[0] = b;
i = 100;
while( Serial.available() < 3 ) { // wait for the rest
delay(1);
if( i-- == 0 ) return 0; // get out if takes too long
}
for( i=1; i<4; i++)
str[i] = Serial.read(); // fill it up
#ifdef DEBUG
Serial.println("header");
#endif
uint8_t sendlen = str[2];
#ifdef DEBUG
Serial.print("cmdlen:"); Serial.println( sendlen, DEC);
#endif
if( sendlen == 0 ) return 0;
i = 100;
while( Serial.available() < sendlen ) { // wait for the final part
delay(1); if( i-- == 0 ) return 0;
}
for( i=4; i<4+sendlen; i++ )
str[i] = Serial.read(); // fill it up
#ifdef DEBUG
Serial.println("got all");
#endif
return 4+sendlen;
}