# Need help with an Arduino program

**URL:** <https://discourse.processing.org/t/need-help-with-an-arduino-program/11306>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [May 16, 2019, 3:08pm UTC](https://discourse.processing.org/t/need-help-with-an-arduino-program/11306 "2019-05-16T15:08:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S.L.C.S](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@S.L.C.S](https://discourse.processing.org/u/S.L.C.S)\
**Post date:** [May 16, 2019, 3:08pm UTC](https://discourse.processing.org/t/need-help-with-an-arduino-program/11306/1 "2019-05-16T15:08:03Z")

</div>

My goal is to write a processing program that turns an LED on or off whenever a “button” (drawn on the screen), is clicked with the mouse.

```auto
int circleX, circleY;  
int circleSize = 93;   
color circleColor, baseColor;
color circleHighlight;
color currentColor;
color circleColour;
boolean circleOver = false;

void setup() {
  size(640, 360);
  circleColor = color(255);
  circleColour = color(0);
  circleHighlight = color(204);
  baseColor = color(0);
  currentColor = baseColor;
  circleX = width/2+circleSize/2+10;
  circleY = height/2;
  ellipseMode(CENTER);
}

void draw() {
  update(mouseX, mouseY);
  background(currentColor);
   fill(255);
  ellipse(circleX, circleY, circleSize, circleSize);

ellipse(100,100, circleSize, circleSize);
}
void update(int x, int y) {
  if ( overCircle(circleX, circleY, circleSize) ) {
    circleOver = true;

  }
}

void mousePressed() {
  if (circleOver) {
    fill(255,0,0); 
    ellipse(100,100, circleSize, circleSize);
  
  }
 if (circleOver){
   fill(255);
}
}
    

boolean overCircle(int x, int y, int diameter) {
  float disX = x - mouseX;
  float disY = y - mouseY;
  if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
    return true;
  } else {
    return false;
  }
  
}
 
    
    

```

So the circle to the right is the button and the circle to the left is the LED. I can’t get the LED to fully turn red when i press the button. I believe if i can get it to turn fully red when the button is pressed i would be able to get to it to turn off when the button is pressed again.

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [May 16, 2019, 3:45pm UTC](https://discourse.processing.org/t/need-help-with-an-arduino-program/11306/2 "2019-05-16T15:45:03Z")

</div>

here it is

```auto
int circleX, circleY;  
int circleSize = 93;   
color circleColor, baseColor;
color circleHighlight;
color currentColor;
color circleColour;
boolean circleOver = false;

void setup() {
  size(640, 360);
  circleColor = color(255);
  circleColour = color(0);
  circleHighlight = color(204);
  baseColor = color(0);
  currentColor = baseColor;
  circleX = width/2+circleSize/2+10;
  circleY = height/2;
  ellipseMode(CENTER);
  textAlign(CENTER, CENTER);
  textSize(20);
}

boolean ledOn = false;
void draw() {
  background(currentColor);
  fill(ledOn ? color(255, 0, 0): color(255));
  ellipse(100, 100, circleSize, circleSize);
  fill(255);
  ellipse(circleX, circleY, circleSize, circleSize);
  fill(0);

  text(ledOn ? "ON" : "OFF", circleX, circleY);
}

void mousePressed() {
  if (overCircle(circleX, circleY, circleSize) && mousePressed) {
    ledOn = !ledOn;
  }
}

boolean overCircle(int x, int y, int diameter) {
  float disX = x - mouseX;
  float disY = y - mouseY;
  if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
    return true;
  } else {
    return false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 16, 2019, 3:47pm UTC](https://discourse.processing.org/t/need-help-with-an-arduino-program/11306/3 "2019-05-16T15:47:57Z")

</div>

or a snappy version

```auto
int circleX, circleY;  
int circleSize = 50;
boolean circleOver = false;
boolean myswitch = false;

void setup() {
  size(640, 360);
  circleX = width/2+circleSize/2+10;
  circleY = height/2;
  ellipseMode(CENTER);
  noStroke();
}

void draw() {
  background(200,200,0);
  fill(200);
  ellipse(circleX, circleY, circleSize, circleSize); // switch
  if ( myswitch ) fill(0,200,0);
  else fill(0,0,200);
  ellipse(100, 100, circleSize, circleSize); // LED
}

void mousePressed() {
  if (overCircle(circleX, circleY, circleSize)) myswitch = ! myswitch;
}

boolean overCircle(int x, int y, int diameter) {
  if (sqrt(sq(x - mouseX) + sq(y - mouseY)) < diameter/2 ) return true;
  else return false;
}

```

and i hope to see the version where you talk to arduino soon!

---

<div class="post-metadata">

**Author:** ![S.L.C.S](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@S.L.C.S](https://discourse.processing.org/u/S.L.C.S)\
**Post date:** [May 16, 2019, 9:17pm UTC](https://discourse.processing.org/t/need-help-with-an-arduino-program/11306/4 "2019-05-16T21:17:46Z")

</div>

Here’s the code that talks to the arduino, but its a bit different.

```auto

const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int ledPin = 13; // LED pin

int button1State, button2State; // variables to hold the pushbutton states

void setup()
{
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);

  // Set up the LED pin to be an output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  // if button1 or button 2 are pressed (but not both)
  if (((button1State == LOW) && (button2State == HIGH)) || ((button1State == HIGH) && (button2State == LOW)))
  {
    digitalWrite(ledPin, HIGH); // turn the LED on
  }
  else
  {
    digitalWrite(ledPin, LOW); // turn the LED off
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 17, 2019, 1:40am UTC](https://discourse.processing.org/t/need-help-with-an-arduino-program/11306/5 "2019-05-17T01:40:13Z")

</div>

hi, thanks,  
well that arduino code ( 2 PB, 1 LED )  
might also be easy to emulate in processing,  
but sorry i misunderstood your wording “talk”  
insofar as i expected a communication between PC+Processing and USB connected Arduino, using the  
[https://processing.org/reference/libraries/serial/Serial.html](https://processing.org/reference/libraries/serial/Serial.html)  
library, that might be too early for you but could be very interesting to operate that  
emulated LED in processing  
by the real arduino buttons? hope to see you there?
