I meanwhile changed in the source code
byte modeMsg[]; byte powerMsg[];
to
char modeMsg[];
char powerMsg[];
now it displays the direction correctly (50, -50, etc.)
but this is still crazy:
this code lets the motor only go reverse (ignores the first 2 commands) and also displays only the reverse direction (-50)
Code: Select all
#define printf2( _x, _y, _format1, _format2, _value1, _value2) { \
string sval1 = FormatNum(_format1, _value1); \
string sval2 = FormatNum(_format2, _value2); \
string s =sval1+sval2; \
TextOut(_x, _y, s); \
}
void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
const byte retLen = 0;
int i, j;
char modeMsg[];
char powerMsg[];
byte devAddr, modeReg, powerReg;
char result;
// constant values for testing
devAddr = 0x02;
modeReg = 0x44 ;
powerReg= 0x45 ;
// Create the message arrays
ArrayBuild(modeMsg, devAddr, modeReg, 0);
ArrayBuild(powerMsg, devAddr, powerReg, percentage);
// Send the first message as soon as the bus is ready
while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
if(I2CWrite(NXTport, retLen, modeMsg) != NO_ERR) return;
printf2(0,56,"mReg=%3d","mVal=%3d",modeMsg[1],modeMsg[2]);
// Send the second message when the first one is done
while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
if(I2CWrite(NXTport, retLen, powerMsg) != NO_ERR) return;
printf2(0,48,"pReg=%3d","pVal=%3d",powerMsg[1],j=powerMsg[2]);
}
task main(){
SetSensorType(S1, SENSOR_TYPE_LOWSPEED);
TXMotorOn(0, 1, 50);
Wait(2000);
TXMotorOn(0, 1, 0);
Wait(1000);
TXMotorOn(0, 1,-50);
Wait(2000);
TXMotorOn(0, 1, 0);
Wait(2000);
while (true);
}
But if I insert sound commands (PlaySound(SOUND_UP)a.s.o.), everything's fine - first forward, then stop, then reverse, then stop, indicated by the sound beeps and the correct display output:
Code: Select all
#define printf2( _x, _y, _format1, _format2, _value1, _value2) { \
string sval1 = FormatNum(_format1, _value1); \
string sval2 = FormatNum(_format2, _value2); \
string s =sval1+sval2; \
TextOut(_x, _y, s); \
}
void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
const byte retLen = 0;
int i, j;
char modeMsg[];
char powerMsg[];
byte devAddr, modeReg, powerReg;
char result;
// constant values for testing
devAddr = 0x02;
modeReg = 0x44 ;
powerReg= 0x45 ;
// Create the message arrays
ArrayBuild(modeMsg, devAddr, modeReg, 0);
ArrayBuild(powerMsg, devAddr, powerReg, percentage);
// Send the first message as soon as the bus is ready
while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
if(I2CWrite(NXTport, retLen, modeMsg) != NO_ERR) return;
printf2(0,56,"mReg=%3d","mVal=%3d",modeMsg[1],modeMsg[2]);
// Send the second message when the first one is done
while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
if(I2CWrite(NXTport, retLen, powerMsg) != NO_ERR) return;
printf2(0,48,"pReg=%3d","pVal=%3d",powerMsg[1],j=powerMsg[2]);
}
task main(){
SetSensorType(S1, SENSOR_TYPE_LOWSPEED);
PlaySound(SOUND_UP);
TXMotorOn(0, 1, 50);
Wait(2000);
PlaySound(SOUND_LOW_BEEP);
TXMotorOn(0, 1, 0);
Wait(1000);
PlaySound(SOUND_DOWN);
TXMotorOn(0, 1,-50);
Wait(2000);
PlaySound(SOUND_LOW_BEEP);
TXMotorOn(0, 1, 0);
Wait(2000);
while (true);
}
what the hell is going on???