Need help with an Arduino program

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.

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.

here it is

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;
  }
}

2 Likes

or a snappy version

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!

2 Likes

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



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
  }
}

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
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?

2 Likes