I recored a video where I show you whats my problem.
Please watch video in 1080p so you can see the warnings and the code
if you don't wish to wast 3 minutes of your life. I'm getting uncomplete midi data according to Hairless and if I check in traktor, the notes constantly switches (see video around 55sec).
Error message from hairless:
Code:
Warning: got a status byte when we were expecting 1(some times it says 2) more data bytes, sending incomplete MIDI Message 0x80 (this last may change also)
p and str are not needed. string is not needed. i is not needed.
pEnd is not needed, either. Use NULL in it's place.
Quote
only 1 more thing, sometimes if I enter for example 03 00 to set blue led to low its takes the 03 for the blue led but also 03 as dimming value or it taks the wrong led, or I have to resend the message again, any idea what cause this and how to fix it,
More meaningful printing would probably reveal where the problem is. But, one thing that you need to keep in mind is that the string functions, like strtol() and strtok() expect strings as arguments, not arrays of characters. The difference between a string and an array of characters is that a string is an array of characters THAT IS NULL TERMINATED. You are not NULL terminating the array of characters, so it is not a string. That can adversely impact the string functions.
Add
Code:
command[6] = '\0';
after the for loop when there is serial data to read.
Quote
also I want my message of (setting red to: 255) only shown 1 time
Then move the } after the if(Serial.available() > 5) farther down. All the string parsing and printing and pin manipulation should only be done when there is serial data received.
I did the things you told me the messages are only shown one time now, added the null
still the problem of I told before still exists
Quote
only 1 more thing, sometimes if I enter for example 03 00 to set blue led to low its takes the 03 for the blue led but also 03 as dimming value or it taks the wrong led, or I have to resend the message again, any idea what cause this and how to fix it,
and I found a new bug, if I do 04 FF(set all leds full power) and than do 04 95 they al go out
Current code:
Code:
int i = 0; char *strings[2]; char * pEnd; char string[32]; char command[6]; int pRed = 4; int pGreen = 3; int pBlue = 2; int led = 0; int power = 0; bool feedback = true; void setup() { Serial.begin(9600); pinMode(pRed, OUTPUT); pinMode(pGreen,OUTPUT); pinMode(pBlue,OUTPUT); }
void loop(){ if (Serial.available() >= 5 ) // wait for 5 characters { for(int i=0; i < 5; i++) command[i] = Serial.read(); command[6] = '\0';
delay(10); led = atoi(strings[0]); power = strtol(strings[1],&pEnd,16); Serial.println(strings[1]); //just for debugging switch(led){ case 1: analogWrite(pRed,power); if(feedback == true){ Serial.print("setting red to: "); Serial.println(power); } led = 0; power = 0; break; case 2: analogWrite(pGreen,power); if(feedback == true){ Serial.print("setting green to: "); Serial.println(power); } led = 0; power = 0; break; case 3: analogWrite(pBlue,power); if(feedback == true){ Serial.print("setting blue to: "); Serial.println(power); } led = 0; power = 0; break; case 4: analogWrite(pRed,power); analogWrite(pGreen,power); analogWrite(pBlue,power); if(feedback == true){ Serial.print("setting all to: "); Serial.println(power); } led = 0; power = 0; break; } } }
Casting a character to an int is not the way to make a number from a string. You've been told how to do it properly. (The atoi() or strtol() functions, depending on whether the text contains HEX characters or not).
that solved it thanks for your help
my code works now
only 1 more thing, sometimes if I enter for example 03 00 to set blue led to low its takes the 03 for the blue led but also 03 as dimming value or it taks the wrong led, or I have to resend the message again, any idea what cause this and how to fix it, also I want my message of (setting red to: 255) only shown 1 time
for ppl who are intressed in my code:
Code:
int i = 0; char *strings[2]; char * pEnd; char string[32]; char command[10]; int pRed = 4; int pGreen = 3; int pBlue = 2; int led = 0; int power = 0;
void loop(){ if (Serial.available() >= 5 ) // wait for 5 characters { for(int i=0; i < 5; i++) command[i] = Serial.read(); }
char *p = command; char *str;
while ((str = strtok_r(p, " ", &p)) != NULL){ // delimiter is the space strings[i] = str; i++; } i = 0;
delay(10); /* //Serial.println("------------------------------"); Serial.println(strings[0]); Serial.println(strings[1]); Serial.println(atoi(strings[0])); Serial.println(strtol(strings[1],&pEnd,16)); Serial.println("------------------------------"); */ led = atoi(strings[0]); power = strtol(strings[1],&pEnd,16); switch(led){ case 1: analogWrite(pRed,power); Serial.print("setting red to: "); Serial.println(power); led = 0; power = 0; break; case 2: analogWrite(pGreen,power); Serial.print("setting green to: "); Serial.println(power); led = 0; power = 0; break; case 3: analogWrite(pBlue,power); Serial.print("setting blue to: "); Serial.println(power); led = 0; power = 0; break; case 4: analogWrite(pRed,power); analogWrite(pGreen,power); analogWrite(pBlue,power); Serial.print("setting all to: "); Serial.println(power); led = 0; power = 0; break;
while ((str = strtok_r(p, " ", &p)) != NULL){ // delimiter is the space
strtok_r() is the threadsafe version of strtok(). On a single-threaded machine, why do you find it necessary to use the more complicated, larger, thread-safe version?
I don't know I'm googleing and I found this, tryed it and it worked I'm noobish to arduino and C/C++ I'm gona try it to let it work with strtok(), gona do some googleing for it
this is the code I have so far, my first 5 sec the data given back is normal after that its al junk
ps. I'm trying to create a rgb led controller over serial 01 FF for red led at 255, 02 DD for green led at 221
Code:
char sz[32] = "01 FF"; int i = 0; char *strings[2]; char * pEnd; char string[32]; char command[10]; int pRed = 4; //I'm using a arduino mega int pGreen = 3; int pBlue = 2; char led=0; int power = 0;
delay(1000);//delay of 1 second Serial.println(strings[0]); Serial.println(strings[1]); Serial.println((int)strtol(strings[0],&pEnd,16)); Serial.println((int)strtol(strings[1],&pEnd,16)); Serial.println("------------------------------"); delay(100); switch((int)strings[0]){ case 1: analogWrite(pRed,(int)strtol(strings[1],&pEnd,16)); led = 0; break; case 2: analogWrite(pGreen,(int)strtol(strings[1],&pEnd,16)); led = 0; break; case 3: analogWrite(pBlue,(int)strtol(strings[1],&pEnd,16)); led = 0; break; }
}
Currently I have no leds working
and my given feedback from the serial monitor is strange
Code:
Ä //havent sent any data to the arduino Ä 0 0 ------------------------------ Ä Ä 0 0 ------------------------------
Ä 0 0 ------------------------------
Ä 0 0 ------------------------------
Ä 0 0 ------------------------------ //sended 01 FF to arduino 01 FF 1 255 ------------------------------ 01 FF 1 255 ------------------------------ // and from here it screws up @ FF 0 255 ------------------------------ @@BEFHÓMMêQQ:RWF
0 0 ------------------------------ @@BEFHqVM½ZQ3RW@@[\^`bdfhjlnprtvxz|~ BEFHÔVM&[QzRW@@[\^`bdfhjlnprtvxz|~prtvxz|~prtvxz|~' 0 0 ------------------------------ // it keeps going on for a endless list of this kind of stuff
I still need to find a methode to get all the data until \n witch wil be something like 01 FF or 02 4D (with the \n on behind it) and split those 2 values from the received string to 2 dec values like 2 (from 02) and 77 (from 4D)
Hello, I'm new to arduino I tryed a few things but every time I'm useing Serial.read(); (print that data) everthing is sended back as ASCII code line by line
I'm trying to create something that I can send "01 FF" to arduino and the 01 part (this is HEX) sets the pin, and "FF" set the analog value of that pin
I tryed a few things but I can't get it to work, and why does he send it back as ASCII code?
I hope someone can help me, I searched the forum and googled this, I can't find something working (I almost got it to work but sscanf only works with a constand value)