// stop watch3 for NXT + BricxCC // by takuya matsubara #include "NXCDefs.h" #define IN_I2C IN_1 //INPUT PORT(1/2/3/4) #define INCNT 0 //sensor to NXT. input bytes byte writebuf[] = {0x02, 0x44,0 }; //send command byte readbuf[] = {0,0}; byte vram[32]; // 01234012340123401234012340123401234012340123401234 string font0=" xxx x xxx xxx x xxxxx xxxxxxxx xxx xxx "; string font1="x x xx x xx x xx x x xx xx x"; string font2="x x x x x x x x x x xx xx x"; string font3="x x x x xxx x x xxxx xxxx x xxx x x"; string font4="x x x x xxxxxx xx x x x x xxxx"; string font5="x x x x x x xx x x x x x "; string font6="x x x x x x x xx x x x x x "; string font7=" xxx xxx xxxxx xxx x xxxx xxx x xxx x "; void put_num(char x,int num) { byte tmp; byte cnt=5; int readcnt = 0; //sensor to NXT. input bytes num *= 5; while(cnt--){ tmp = 0; if(SubStr(font0,num,1) !=" ") tmp |= (0x01 << 0); if(SubStr(font1,num,1) !=" ") tmp |= (0x01 << 1); if(SubStr(font2,num,1) !=" ") tmp |= (0x01 << 2); if(SubStr(font3,num,1) !=" ") tmp |= (0x01 << 3); if(SubStr(font4,num,1) !=" ") tmp |= (0x01 << 4); if(SubStr(font5,num,1) !=" ") tmp |= (0x01 << 5); if(SubStr(font6,num,1) !=" ") tmp |= (0x01 << 6); if(SubStr(font7,num,1) !=" ") tmp |= (0x01 << 7); vram[x] = tmp; x++; num++; } } void put_vram(void) { int i,j; ClearScreen(); for(i=0;i<(100/4);i++){ for(j=0;j<8;j++){ if((0x80>>j)& vram[i]){ RectOut(i*4, j*6, 3, 5); } } } } void put_time(int time) { put_num( 0,(time / 1000)%10); put_num( 6,(time / 100)%10); put_num(14,(time / 10)%10); put_num(20,time % 10); put_vram(); } void wait_button_press(void){ while(ButtonPressed(BTNCENTER, false)==1); //until OFF while(ButtonPressed(BTNCENTER, false)==0); //until ON } #define THRESHOLD 30 task main() { int time; long nexttick; int btn_disable; int a1,a2,b1,b2; int readcnt = INCNT; byte chrcode; SetSensorLowspeed (IN_I2C); TextOut(0,LCD_LINE1,"Stop Watch"); TextOut(0,LCD_LINE2,"IN1:NXT serial"); TextOut(0,LCD_LINE3,"IN2:(light)start"); TextOut(0,LCD_LINE4,"IN3:(light)end"); TextOut(0,LCD_LINE5,"Press Center Btn"); wait_button_press(); vram[12]=0x80; //point SetSensorType(S2, SENSOR_TYPE_LIGHT_ACTIVE); SetSensorType(S3, SENSOR_TYPE_LIGHT_ACTIVE); Wait(1000); while(1){ PlayTone(200,50); a1 = Sensor(S2); b1 = Sensor(S3); time = 0; put_time(time); TextOut(0,LCD_LINE1,"READY..."); while(1){ a2 = Sensor(S2); if(abs(a2-a1)>THRESHOLD)break; } PlayTone(400,50); nexttick = CurrentTick(); ClearScreen(); TextOut(0,LCD_LINE2,"START"); btn_disable=1; while(1){ b2 = Sensor(S3); if(abs(b2-b1)>THRESHOLD)break; } time = CurrentTick() - nexttick; time /= 10; if(time > 9999)time=9999; put_time(time); writebuf[2] = (time / 1000)%10 + '0'; I2CBytes(IN_I2C, writebuf, readcnt, readbuf); Wait(20); writebuf[2] = (time / 100)%10 + '0'; I2CBytes(IN_I2C, writebuf, readcnt, readbuf); Wait(20); writebuf[2] = '.'; I2CBytes(IN_I2C, writebuf, readcnt, readbuf); Wait(20); writebuf[2] = (time / 10)%10 + '0'; I2CBytes(IN_I2C, writebuf, readcnt, readbuf); Wait(20); writebuf[2] = time%10 + '0'; I2CBytes(IN_I2C, writebuf, readcnt, readbuf); Wait(20); writebuf[2] = 13; I2CBytes(IN_I2C, writebuf, readcnt, readbuf); Wait(20); writebuf[2] = 10; I2CBytes(IN_I2C, writebuf, readcnt, readbuf); Wait(20); PlayTone(500,50); TextOut(0,LCD_LINE1,"PUSH BUTTON"); wait_button_press(); } }