Hello all.
I’ve been programming at the hobby level on and off for years but I’m new to Arduino. I’ve never programmed professionally as the code ahead will show.
I’ve physically connected Sparkfun’s Promini pack to the MiP robot by Wowwee.
SparkFun MiP ProMini-Pack
Wowwee MiP
I’m able to send commands to the MiP to make its head and chest blink and change the volume. Before I forge ahead much further I want to be able to get data out of the MiP. For now I’m just trying to implement a getVolume function.
To make things as simple for myself as I can I’ve written the following sketch. I set the volume to 2, then try to read it back.
const int8_t _UART_Select_S = 2;
const int8_t _UART_Select_R = 1;
const int8_t MIP_SET_VOLUME_CMD = 0x15;
const int8_t MIP_GET_VOLUME_CMD = 0x16;
const int8_t MIP_VOLUME_LVL = 0x02;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(_UART_Select_S, OUTPUT);
pinMode(_UART_Select_R, INPUT);
uint8_t array_length = 9;
uint8_t data_array[] = "TTM:OK\r\n";
sendMessage(data_array, array_length);
uint8_t set_volume[] = {MIP_SET_VOLUME_CMD, MIP_VOLUME_LVL};
sendMessage(set_volume, 2);
Serial.println("SETUP");
delay(500);
}
void loop() {
uint8_t question[] = {MIP_GET_VOLUME_CMD};
sendMessage(question, 2);
}
void sendMessage(unsigned char *message, uint8_t array_length) {
digitalWrite(_UART_Select_S, HIGH);
delay(5);
uint8_t i = 0;
for (i; i < array_length; i++) {
Serial.write(message[i]);
}
Serial.write(0x00);
delay(5);
digitalWrite(_UART_Select_S, LOW);
}
void serialEvent() {
uint8_t answer[5];
int bytes = Serial.available();
Serial.print("Bytes read: ");
Serial.print(bytes);
Serial.print("\n");
digitalWrite(_UART_Select_R, HIGH);
for (int i = 0; i < bytes; i++) {
answer[i] = Serial.read();
Serial.println(answer[i], HEX);
}
digitalWrite(_UART_Select_R, LOW);
// The delay makes it easier for the human to read output on the serial monitor.
delay(3000);
}
I expect to see four bytes coming back: 31,36,30 and 32. That would be the “16” (get volume command that I asked MiP for) and “02”, as in the volume level. The above code produces the following results.
SETUP
Bytes read: 4
31
36
30
32
Bytes read: 4
31
36
30
32
Bytes read: 4
31
36
30
32
Bytes read: 4
31
36
30
E6
Bytes read: 3
31
13
92
So, you see, I’m getting the right answer, but only sometimes. I’m not sure in which direction I’m supposed to go to fix this. Am I doing something wrong? Obviously. Am I supposed to fix this with some kind of error detection/correction or is there something I can do to assure my code is getting what I expect?
As I said, I’m new to Arduino so if something looks completely ridiculous in my code, feel free to laugh out loud then correct me.
Thank you!