Arduino switch with Processing

edited April 2014 in Arduino

Hey guys, I have code that uses a button and when pressed will switch on an LED(which will stay on) and then when the button is pressed again, the LED is turned off. I have connected the Arduino and Processing using Standard Firmata successfully and the LED works.

What I want to be able to do is that when the button is pressed, an image will show up in Processing but also stay on the screen, just like the LED. I'm just testing it with shapes for now. I would really appreciate your help. This is what I have so far:

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

int buttonPin = 7;
int buttonState = 0;
int buttonPressed = 0;

void setup(){ 
  size(600, 600);
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[6], 57600);
  arduino.pinMode(buttonPin, arduino.INPUT);  
}

void draw()
{
  buttonState = arduino.digitalRead(buttonPin);

  if (buttonState == arduino.HIGH && buttonPressed == 0)
  {
    buttonPressed = 1;
    rect(10, 10, 10, 10); 
    text("hello", 10, 10);
  }
  if (buttonState == arduino.LOW && buttonPressed == 1)
  {
    buttonPressed = 0;
    rect(50, 50, 10, 10); 
  }
} 

Answers

  • You can use following lines in place of shapes.

    PImage ad;                // Add this as global declaration
    
    ad = loadImage("Image.jpg");  // Add this in setup()
                                                  // Image.jpg should be present in data folder
    
    image(ad, x, y);                       //Call this in place of rect()
                                             // x, y are co-ordinates for the upper-left corner of image
    

    Hope this would help you.

  • hi....i am new and only familiar with processing. Arduino is totally new for me. You already worked wit data from arduino to manupilate an image in processing? is it possible to give me an example from arduino and how to import data in processing. I want to use the sensor HC-sr 04 in arduino to let move images in processing. I hope you can help me to go on.

Sign In or Register to comment.