Problem writing from Processing

Hello,

I'm relatively new to programming in general, especially to Processing and Arduino, but I've been having some trouble that people experienced with Arduino have been having trouble with.

I'm currently attempting to do a relatively simple program that has an LED blink in a certain rhythm. This is the part of the code from Processing:

     if (counter % check == 0)
     {
     println("LET THERE BE LIGHT");
     if (counter % 3 == 0)
     {
     port.write("1");
     }
     else
     {
       port.write("2");
     }
     }

I have tested each if statement, and they all do get called in regular intervals.

And here's the Arduino file:

void setup()
{
  Serial.begin(9600);
  pinMode(13,OUTPUT);
}

void loop()
{
  while(Serial.available())
  {
    byte a;
    a = Serial.read();
    if (a=='1')
    {
        analogWrite(13,255);
        delay(100);
    }
    else if(a=='2')
    {
        analogWrite(13,127);
        delay(100);
    }
    digitalWrite(13,LOW);
    delay(567);
  }
        
}

I have also tested this portion of the file using the Serial Monitor; input from there works exactly as I want it to.

However, uploading the Arduino file and running Processing results in the LED either remaining eternally on or eternally off (with the reset button doing nothing as well). So the problem must be in the writing from processing to arduino, but I can't see what is wrong (I also tried treating the input as chars).

Can anyone see what I'm doing wrong?

Most of the time, your Serial.read will return -1, so you'll write LOW (0) to pin 13.

Even though I wrote "1" or "2"?

If that's the case, how do I get around that problem?

Use "Serial.available"

Edit: Scratch that - it doesn't matter what you read, you always write zero anyway.
Plus, pin 13 doesn't support PWM

Then is there any way to get it to do two different levels of brightness based on a parameter?

Yes, you could implement PWM in software.
Or put a LED on a pin that does support PWM.

Excuse my ignorance, but what's PWM?

Pulse Width Modulation - the ability dim a LED or control the speed of a motor.
The output of an "analogWrite".
Only certain pins on the Arduino can provide hardware PWM, depending on board type.
Check the reference page for analogWrite for which pins your board supports.

Well I know it can dim from testing out of the Serial Monitor. The problem is communication between Processing and Arduino. Even replacing the Arduino file with a digitalWrite LOW still results in my light being constantly on, despite the loops being called.

Even replacing the Arduino file with a digitalWrite LOW still results in my light being constantly on, despite the loops being called.

You need to post your latest code. If you are setting a pin LOW, and the LED continues to be on, you have a hardware problem. A photo of your setup, in focus and of moderate size, would be useful.

The new arduino code is nothing complicated at all. Same setup() as before:

if (Serial.available()) //Also tried while
{
Serial.read(); //also tried without this
digitalWrite(13, LOW);
delay(100);
}

It still stays perpetually on, and again, the condition in the loops in the Processing file are satisfied. It shouldn't be a hardware issue, as the Serial Monitor had it blink correctly.

I attached a photo of the arduino board - nothing complicated at all. Hopefully it's clear enough.

If that code does not cause the LED to go off, it means that Processing is not sending any serial data to the Arduino. You should be able to see the RX light on the Mega blink when data arrives. If it is not blinking, data is not arriving.

Post all of your Processing application, not just the snippet you think the problem might occur in.

Also, you REALLY need a resistor with that LED. LEDs will suck a lot more current than the Arduino can safely supply, unless you use a current limiting resistor.

Full file:

import processing.serial.*;
import java.math.*;
Serial port;

String [][] csv;
int csvWidth=0;
int startingTime;
long m = millis();
int counter=0;
int tempo = 0;
double BPM = 0;
int gcd = 0;
int timeSignatureNum = 0;
int timeSignatureDen = 0;

void setup()
{
  String lines[] = loadStrings("DRIPiano.csv");

  println(Serial.list());
  port = new Serial(this, Serial.list()[0],9600);
  
  //Find width of CSV
  for (int i=0; i < lines.length; i++) 
  {
    String [] chars = split(lines[i],',');
    if (chars.length>csvWidth)
    {
      csvWidth=chars.length;
    }
  }
  //Create CSV array
  csv = new String [lines.length][csvWidth];
  //PLace values into 2D array
  for (int i=0; i < lines.length; i++) 
  {
    String [] temp = new String [lines.length];
    temp= split(lines[i], ',');
    for (int j=0; j < temp.length; j++)
    {
       csv[i][j]=temp[j];
    }
  }
    startingTime = millis(); 
    
    tempo = Integer.parseInt(csv[3][3]);
    BPM = (double)(120)/500000*tempo;
    
    gcd = gcd((int)BPM, tempo);
    
    frameRate((int)BPM/gcd);
    //Ex. If BPM = 90, tempo = 60, 2/3 second per beat
    //So gcd = 30, 90/30 = 3; so we want to count frames
    //By 1/3rd
    }

void draw()
{
  
    int m = (millis() - startingTime);
    println(m);
     
     counter ++;
     int check = 60/gcd;
     if (counter % check == 0)//60 because we want seconds, 60/BPM = secs per beat
     {
     println("LET THERE BE LIGHT");
     if (counter % 3 == 0)
     {
     port.write("1");
     }
     else
     {
       port.write("2");
     }
     }
     
}


 public static int gcd(int a, int b) {
 
   if (b==0) 
     return a;
   else
     return gcd(b, a % b);
 }
    gcd = gcd((int)BPM, tempo);

You have a variable and a function with the same name. That's a great idea. NOT.

     int check = 60/gcd;

The value of gcd does not change in draw(). Move this to setup, and print out the value. It may not be what you think it is.

On every pass through loop, you increment counter. There is no fixed length of time between updates to counter. Is this really what you want?

  port = new Serial(this, Serial.list()[0],9600);

Are you certain that the Arduino is connected to the first item in the list?

Moved check to setup, equals 2 as I expected.

I set it to increment thrice every second, which the println of the millis shows to be true.

I turned the 0 into a 1 in that port declaration and got an arrayoutofbounds error, so it's the the first.

By the way, if this helps at all the only port available to connect to on the computer is COM3.

Is COM3 the port that the Arduino IDE uploads to?

Tools -> Serial Port -> COM3 is checked

Is it possible that the Arduino device isn't looking for input, or that it somehow gets lost in communication from Processing?

Nice picture. :stuck_out_tongue: It is neither sharp nor "medium" sized.... Give the quality it almost looks like you have put the LED in the wrong holes between AREF and GND not between GND and pin13. If you upload the "Blink" sketch does it blink? Or even simpler - is your green LED blinking when the onboard yellow pin13 LED is blinking? (In fact, you could remove your LED and just look at the onboard pin13yellow LED)

Yu have not answered PaulS' question - can you see the upload TX light blink (the yellow LED that blinks when you upload) when your program is running? If it remains stubbornly dark, the Processing is not sending.