Processing + Arduino library: fading function not working

Hello, I'm new here because I am more an Arduino-man, but I know a little little bit of Processing to support these projects. I'd like to make this project with my Arduino: YouTube movie. So I want to make an RGB led strip react to my music, and I'll add a lot of other functions. Everything will be controlled with a remote controll. But now: what is my question? Before I order my led strip, I want to check wether I will get a nice result with Processing. The original script to put on my Arduino Uno R3 is:

#define REDPIN 5  
#define GREENPIN 6
#define BLUEPIN 3

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup()
{
  pinMode(7,INPUT); //SIG of the Parallax Sound Impact Sensor connected to Digital Pin 7
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
  redNow = random(255);
  blueNow = random(255);
  greenNow = random(255);
  redNew = redNow;
  blueNew = blueNow;
  greenNew = greenNow;

}

#define fade(x,y) if (x>y) x--; else if (x<y) x++;

void loop()
{
  boolean soundstate = digitalRead(7);
  if (soundstate == 1) {
    analogWrite(BLUEPIN, blueNow);
    analogWrite(REDPIN, redNow);
    analogWrite(GREENPIN, greenNow);
    redNew = random(255);
    blueNew = random(255);
    greenNew = random(255);
    // fade to new colors
    while ((redNow != redNew) ||
      (blueNow != blueNew) ||
      (greenNow != greenNew))
    {
      fade(redNow,redNew)
        fade(blueNow,blueNew)
          fade(greenNow,greenNew)
            analogWrite(BLUEPIN, blueNow);
      analogWrite(REDPIN, redNow);
      analogWrite(GREENPIN, greenNow);
      delay(1);
    }
  }
  else{
    digitalWrite(REDPIN,0);
    digitalWrite(GREENPIN,0);
    digitalWrite(BLUEPIN,0);
  }
}

So I want to tweak this code to make a square fade on the music on my screen (using Processing), just like the LED strip would do. I want to use the Arduino Library.

This is what I made of it:

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup()
{
    arduino = new Arduino(this, "COM10", 57600);
  size(800,800);
  arduino.pinMode(9,Arduino.INPUT); //SIG of the Parallax Sound Impact Sensor connected to Digital Pin 7

  redNow = int(random(255));
  blueNow = int(random(255));
  greenNow = int(random(255));
  redNew = redNow;
  blueNew = blueNow;
  greenNew = greenNow;

}

void fade(int x,int y) 
{
  if (x>y) x--; else if (x<y) x++;
}

void draw()
{
  int soundstate = arduino.digitalRead(9);
  if (soundstate == 1) {
    fill(redNow,greenNow,blueNow);
    rect(10,10,780,780);
    redNew = int(random(255));
    blueNew = int(random(255));
    greenNew = int(random(255));
    // fade to new colors
    while ((redNow != redNew) ||
      (blueNow != blueNew) ||
      (greenNow != greenNew))
    {
      fade(redNow,redNew);
      fade(blueNow,blueNew);
      fade(greenNow,greenNew);
      fill(redNow,greenNow,blueNow);
      rect(10,10,780,780);
      delay(1);
    }
  }
  else{
     fill(0);
     rect(10,10,780,780);
      }
}

This doesn't work. It just gives me a white screen. But this very much simplified code does work: it gives me other colors based on the music (or when I clapp, speak...)

Arduino arduino;

void setup() {
  size(470, 280);
  println(Arduino.list());

  arduino = new Arduino(this, "COM10", 57600);

  arduino.pinMode(9, Arduino.INPUT);
}

void draw() {

int val = arduino.digitalRead(9);
  if (val == 1)
  {

  int rood = int(random(255));
  int groen = int(random(255));
  int blauw = int(random(255));
  fill(rood,groen,blauw);
  rect(10,10,450,260);
  delay(1);
   }
}

So I guess there's something wrong with my fade function or so? I really don't know. I'd be very greatful if someone could help me out :)

Thanks a lot! Bram

Sign In or Register to comment.