0
votes

What I want to do is to control 2 leds in the arduino board with a processing program. If I press any point on the upper half of the screen the led (Pin 13) will light on, and if I press a point on the lower half of the screen, it will turn on other led (Pin 12). So, I programmed 2 buttons, with 2 leds(Pin 12 an 13) and no matter what button I press, It always turns on pin 13. I made a separate experiment, with only 1 button, change only the pin 13 by 12. It does not work, always turns on pin 13.

ARDUINO CODE:

boolean estado;
boolean estado1;
byte a; 
void setup()
{
  Serial.begin(9600);
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  pinMode (13, OUTPUT);
  digitalWrite (13, LOW);
  randomSeed(analogRead(0));
  estado = false;
  estado1 = false;
}
void loop()
{
    delay(100);
    Serial.write(random(40));
    while(Serial.available() > 0)
    {
      a = Serial.read();
      if (a == 0)
        {
        estado = !estado;
        digitalWrite(12, estado);
        }
      if (a == 1)
        {
        estado1 = !estado1;
        digitalWrite(13, estado1);
        }
    }
}

PROCESSING CODE:

void compruebaBoton()
{
  if( mouseY < 640) 
  {
    try
    {
      ons.write(0);
    }
    catch(Exception ex)
    {
      estado = 4;
      error = ex.toString();
      println(error);
    }
  }

 if( mouseY > 640)
  {
    try
    {
      ons.write(1);
    }
    catch(Exception ex)
    {
      estado = 4;
      error = ex.toString();
      println(error);
    }
  }
}

the code I am implementing is correct for what I am trying to do?

1
estado1 is not initialized before setup() ? - Faraz Ahmed
Sorry. My bad. I did not copy that line. - Andrea Diaz

1 Answers

1
votes

After playing with the code for hours, I found the problem!! The program is not loaded into the arduino if the bluetooth board is connected to the arduino board.