Go Down

Topic: I am facing problems with encoding (Read 444 times) previous topic - next topic

saurabhorange

Sep 06, 2012, 01:30 pm Last Edit: Sep 06, 2012, 07:41 pm by CrossRoads Reason: 1
I have Arduino Leonardo and controlling a RGB LED using Vb.net

So far i have managed to do for single colors only (like red, green blue white etc) Now i want the user to send rgb values and correspondingly the led lights

The problem is that is vb.net serialport component writes data in string or byte array if i write data as string 1 becomes 49 which i dont want so can  anyone tell me which encoding should i use for string so that 1 remains 1 only

here is my very basic code so far and i have one more problem which led fades and user tries to stop it by sending 0 value it dosnt stop it keeps fading
Code: [Select]

(code tags added by moderator)
int r = 11; // red pin no.

int g = 10; // green pin no.
int b = 9; // blue pin no.

#define delayTime 20

int rVal = 0; // initial red value
int gVal = 0; // initial green value
int bVal = 0; // initial blue value

int i =0;



void setup() {

 pinMode(r,OUTPUT); // initialize red pin as output pin
 pinMode(g,OUTPUT); // initialize green pin as output pin
 pinMode(b,OUTPUT); // initialize blue pin as output pin


Serial.begin(9600); // begin serial communication at 9600 Rate

}


void loop()

{

if (Serial.available() > 0)  // if data is available for processing

{
 

int data = Serial.read() - 48;


if ( data == 1) {
 
 
  analogWrite(r,255);
  analogWrite(g,0);
  analogWrite(b,0);
 
}

else if (  data == 2)


{
  analogWrite(r,0);
  analogWrite(g,255);
  analogWrite(b,0);
}
 
 
 
 else if (  data == 3)


{
  analogWrite(r,0);
  analogWrite(g,0);
  analogWrite(b,255);
}
 
   else if (  data == 4)


{
  analogWrite(r,255);
  analogWrite(g,255);
  analogWrite(b,255);
}
 
  else if ( data== 0) {
   
     analogWrite(r,0);
  analogWrite(g,0);
  analogWrite(b,0);
 
  loop();
 
  }
 
else if (data ==5) {
 
  fade();
 
 
 
 
}
    else if (data ==7) {
     
     
     
     
     int data = Serial.read();
     
         analogWrite(r,data);
  analogWrite(g,0);
  analogWrite(b,0);
     
     
    }
   
    else if ( data == 10 )
{
 
 int r1  = Serial.parseInt();
 
 
 
 analogWrite(r,r1);
 
   analogWrite(g,0);
  analogWrite(b,0);
 
}

 
}


 
 
}



void  fade() {
 
  while ( Serial.available() == 0 )

  {
   
   
   int redVal = 255;
 int blueVal = 0;
 int greenVal = 0;
 for( int i = 0 ; i < 255 ; i += 1 ){
     if (Serial.available() > 0 ) {
     
    loop();
   

     
   }
 
   greenVal += 1;
   redVal -= 1;
   analogWrite( g,  greenVal );
   analogWrite( r,  redVal );
 if (Serial.available() > 0 ) {
     
    loop();
   

     
   }
   delay( delayTime );
   
 }

 redVal = 0;
 blueVal = 0;
 greenVal = 255;
 for( int i = 0 ; i < 255 ; i += 1 ){
   
   if (Serial.available() > 0 ) {
     
     loop();
     
     
     
     
     
   }
   blueVal += 1;
   greenVal -= 1;
   analogWrite( b,  blueVal );
   analogWrite( g,  greenVal );
 if (Serial.available() > 0 ) {
     
    loop();
   

     
   }
   delay( delayTime );
 }

 redVal = 0;
 blueVal = 255;
 greenVal = 0;
 for( int i = 0 ; i < 255 ; i += 1 ){
   
   if (Serial.available() > 0 ) {
     
    loop();
   
     
     
     
   }
   redVal += 1;
   blueVal -= 1;
   analogWrite( r,  redVal );
   analogWrite( b,  blueVal );
 if (Serial.available() > 0 ) {
     
    loop();
   

     
   }
   delay( delayTime );

 }
 
 
  }
 
}

johnwasser

If you are sending character codes you should compare against character codes.

Code: [Select]

if ( data == '1') {


NOTE: You should NOT call loop() from fade().  This will fill up memory with return addresses that are never used and cause a crash.  To go back into loop() from fade() you should use "return;".
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

saurabhorange

Update : I did it!!! I used Serial.parseint function of arduino and everything worked like magic and on vb.net side i sent strings like 255,255,255

bUT I WOULD STILL LOVE IF SOMEONE GIVES A GOOD EXPLANATION OF ENCODING AND SERIAL DATA

and one more question : I am seeing that not all rgb codes work as we see on computer screen can this be fixed I have good quality piranha led super flux 5 mm

Go Up