Help with blink code...

I just got my Duemilanove in yesterday and I did not buy anything to go with it. I am just messing around and trying to refresh my mind in coding from when I was in highschool (almost 10 years ago now)... Anyway, I tried to modify the code for the dimmer example (on the arduino website) using both Arduino and Processing so that instead of dimming the on board LED it will blink faster or slower based upon mouse position. I cannot get this to work and I've tried many combinations, if anyone could correct my code and explain why it was wrong, I would greatly appreciate it.

arduino dot cc slash en slash Tutorial slash Dimmer

int ledPin = 13;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  byte val;
  int val2;
  

  // check if data has been sent from the computer
  if (Serial.available()) {
    
    // read the most recent byte (which will be from 0 to 255)
    val2 = val*100;
    val = Serial.read();
    
    // blinks based upon mouse position
    digitalWrite(ledPin, HIGH);
    delay(val2);
    digitalWrite(ledPin, LOW);
    delay(val2);
  }
}

Sorry if this is in the wrong section, mods please move it if it is.

Two things I can see:

  1. Move

byte val; int val2;

out of void loop() and put them at the beginning with

int ledPin = 13

  1. These two lines are in the wrong order

val2 = val*100; val = Serial.read();

I suspect they should be

val = Serial.read(); val2 = val*100;

because val needs to be set before you can multiply it to get val2.

Try that and see how it goes.

Ok, that helped a bit and got me closer to what i want... problem now is, it doesn't seem that the values are being reset properly. When the app starts, the blink rate is very fast, then i move my mouse onto the gradient area that Processing draws... the blink rate changes, and I can get it to change slowly moving from left to right, but it doesn't get fast again moving back to the left... something is still a little off. Thanks for the help!

int ledPin = 13;
byte val;
int val2;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  // check if data has been sent from the computer
  if (Serial.available()) {
    
    // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
    val2 = val*10;
    
    // blinks based upon mouse position
    digitalWrite(ledPin, HIGH);
    delay(val2);
    digitalWrite(ledPin, LOW);
    delay(val2);
    val2=0;
  }
}

Well your code looks OK now. Can you do a Serial.print() when you’re using processing? If so, have it send you val and val2 so you can see what they’re being set to. If they’re not changing or they’re changing too quickly, then you may need to look at the processing side.

I'm sorry for the noob questions, and I grasp kind of what you're saying. You're asking that I get the output of my variables so I can see what they're doing to make sure they're behaving as I want them to.

Can you explain a little better how to put this into my program so I can see it's output? Will it print to the console within the arduino IDE or within the Processing IDE? Or.... does it need to be somehow written to a text file? I know this sounds like a lot of work for a seemingly simple task, but my goals for even learning how to do this stuff are going to require a lot of problem solving on my part to reach them.... and even help on little stuff like this is going to help me tremendously later on. Kudos to you! ;)

I've never used processing, so I'm not sure what to tell you. But in a general sense, if processing can send serial data to the Arduino board, it's probably able to read it as well. You need to figure out a way to make processing display the serial data coming from the Arduino board.

The Arduino IDE will certainly display the serial data coming from the board. But I doubt it will do that when processing is talking to the board. So I think you will have to make it work entirely in processing.

And now I'm curious about processing. So I think I'll download it and check it out.... :)

I made a little progress, I am at least getting Processing to print the val2 variable… which wasn’t hard to do…

// Dimmer - sends bytes over a serial port
// by David A. Mellis

import processing.serial.*;

Serial port;
int val;

void setup()
{
  size(256, 150);

  println("Available serial ports:");
  println(Serial.list());

  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[0], 9600);  

  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
}

void draw()
{
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, i, 150);
  }

  // write the current X-position of the mouse to the serial port as
  // a single byte
  val=port.read();
  port.write(mouseX);
  println(val);
}

and the Arduino code to send the variable:

int ledPin = 13;
byte val;
int val2;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  // check if data has been sent from the computer
  if (Serial.available()) {
    
    // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
    val2 = val*10;
    Serial.write(val2);
    
    // blinks based upon mouse position
    digitalWrite(ledPin, HIGH);
    delay(val2);
    digitalWrite(ledPin, LOW);
    delay(val2);
    val2=val;
  }
}

So that’s dumping the variable to the console in Processing, but I’m getting a LOT of -1’s and very few variables otherwise…

Glad I could kick start you on Processing, and also I’m glad that I jumped right into the nitty gritty stuff even before having anything to hook to my arduino… it’s these kinds of practices that are going to most relate to what I’m trying to do. :smiley: