Serial data sending doesn't work...

Hi, this is my code:

int z = 0;
float h;
int h_int;
int val = 0;
int fdelay = 20;
int R = 10;
int G = 6;
int B = 9;
int rprev = 0;
int gprev = 0;
int bprev = 0;
void setup(){
  Serial.begin(115200);
  pinMode(12, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
}

void h2rgb(float H, int& R, int& G, int& B) {

  int var_i;
  float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

  if ( S == 0 )                       //HSV values = 0 ÷ 1
  {
    R = V * 255;
    G = V * 255;
    B = V * 255;
  }
  else
  {
    var_h = H * 6;
    if ( var_h == 6 ) var_h = 0;      //H must be < 1
    var_i = int( var_h ) ;            //Or ... var_i = floor( var_h )
    var_1 = V * ( 1 - S );
    var_2 = V * ( 1 - S * ( var_h - var_i ) );
    var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

    if      ( var_i == 0 ) {
      var_r = V     ;
      var_g = var_3 ;
      var_b = var_1 ;
    }
    else if ( var_i == 1 ) {
      var_r = var_2 ;
      var_g = V     ;
      var_b = var_1 ;
    }
    else if ( var_i == 2 ) {
      var_r = var_1 ;
      var_g = V     ;
      var_b = var_3 ;
    }
    else if ( var_i == 3 ) {
      var_r = var_1 ;
      var_g = var_2 ;
      var_b = V     ;
    }
    else if ( var_i == 4 ) {
      var_r = var_3 ;
      var_g = var_1 ;
      var_b = V     ;
    }
    else                   {
      var_r = V     ;
      var_g = var_1 ;
      var_b = var_2 ;
    }

    R = (1-var_r) * 255;                  //RGB results = 0 ÷ 255
    G = (1-var_g) * 255;
    B = (1-var_b) * 255;
  }
}

void loop()
{
   if(z == 1){
     if(val < 1023){
     val = val++;
     delay(fdelay);
    h = ((float)val)/1024;
    h_int = (int) 360*h;
    int redvar;
    int bluevar;
    int greenvar;
    h2rgb(h, redvar, greenvar, bluevar);
    analogWrite(R, redvar);
    analogWrite(G, greenvar);
    analogWrite(B, bluevar);
    }
    else{ val = 0; }
   }
  char c, t;
   int nnn;

   if(Serial.available() > 0) // If there is data to read
   {
       c = Serial.read(); // Get a character

       delay(50);

       if(c >= 'a' && c <= 'z') // If it's a command
       {
          nnn = 0;
          while(Serial.available() > 0) // While there is still data
          {
             t = Serial.read(); // Read next character

             if(t == ',' || t == ' ')
               continue; // Skip commas and spaces
             if(t >= '0' && t <= '9') // If it's a number
             {
                nnn *= 10; // Multiply previous value by 10
                nnn += t - '0'; // Add the new digit
             }
             else
                break;

             delay(50);
          }
       }
   }

   // Now, do something with c and nnn
   switch(c)
   {
      case 's':
        analogWrite(5,nnn);
        break;
      case 'f':
        analogWrite(3,nnn);
        break;
        case 'u':
          if (nnn > 100){
            digitalWrite(12, HIGH);
          }
          if (nnn < 100){
            digitalWrite(12, LOW);
          }
          break;
        case 'r':
          analogWrite(R,nnn);
          rprev = nnn;
          break;
        case 'g':
          analogWrite(G,nnn);
          gprev = nnn;
          break;
        case 'b':
          analogWrite(B,nnn);
          bprev = nnn;
          break;
        case 'z':
          if(nnn > 100){
            z = 1;
          }
          if(nnn < 100){
            z = 0;
          }
          break;
        case 'x':
          fdelay = nnn;
          break;
        case 'l':
          lightning();
          break;
        case 'a':
          analogWrite(3, nnn);
          analogWrite(6, nnn);
          analogWrite(9, nnn);
          analogWrite(10, nnn);
          analogWrite(11, nnn);
          digitalWrite(12, LOW);
          break;
      // Add other letters here, with break after each one
   }
}

void lightning(){
  int del = 0;
  analogWrite(R, 255);
  analogWrite(G, 255);
  analogWrite(B, 255);
  del = random(10, 150);
  delay (del);
  analogWrite(R, 0);
  analogWrite(G, 0);
  analogWrite(B, 0);
  del = random(10, 150);
  delay(del);
  analogWrite(R, 255);
  analogWrite(G, 255);
  analogWrite(B, 255);
  del = random(10, 150);
  delay(del);
  analogWrite(R, 0);
  analogWrite(G, 0);
  analogWrite(B, 0);
  del = random(10, 150);
  delay(del);
  analogWrite(R, 255);
  analogWrite(G, 255);
  analogWrite(B, 255);
  del = random(10, 150);
  delay(del);
  analogWrite(R, 0);
  analogWrite(G, 0);
  analogWrite(B, 0);
  delay(20);
  analogWrite(R, rprev);
  analogWrite(G, gprev);
  analogWrite(B, bprev);
 }

It used to work, however now my switch doesn’t seem to work.

I put some Serial.println’s around the code, and everything is still working, including the loop, however the Arduino doesn’t seem to be receiving the serial data correctly anymore. The baud rates are correct, comports are fine etc, but it’s not receiving data! Well, not the right data in any case!

However, I think my problems may come from a recent driver installation. I have another device which also uses the FTDI interface, so maybe the drivers I installed for that are interfering with the Arduino?

Anyone got any idea’s I can try?

Regards,
Dan

Does uploading a Sketch work?

Yes. When I send something to the Arduino, the RX light flashes, however I wrote a script that basically echo's what it receives, and it just continuously reports -1. It doesn't even attempt to send back, I'm not sure if the Arduino is even receiving what it should?!

EDIT: Also, it's not the Arduino's fault, it does the same thing on both of my boards.

EDIT: Also, for some reason, when I send it something while it's continuously echoing -1, it stops transmitting back as well!

Would installing drivers for a different FTDI device cause problems like this? I'm not sure how to uninstall them to check though ..

Would installing drivers for a different FTDI device cause problems like this?

That's certainly possible.

However, AVRDUDE uses the same serial port to upload a Sketch that you're using to communicate with the Sketch. It seems much more likely that the serial port (and driver) are fine; that the problem lies elsewhere.

I'm not sure how to uninstall them to check though

Which operating system?

I'm using Windows 7

I tried completely uninstalling the FTDI drivers and letting Windows reinstall them, but I'm still having the same problem. Also, the problem occurs no matter how I send the serial data. Neither the serial monitor or a VB app works :/

Hmm, hang on a minute, I think I may know the problem.

Aha, yep, found it.

I had a modified version of hardwareserial.cpp I had been using for a DMX rciever with the Arduino. Stupid me forgot to put the original back :-[ :-[

Thanks for the help, I should learn to think harder next time :-X

Dan

How embarassing. I've certainly never made a mistake like that. ::) (today) ;D