Thank you Abel
I have succeeded to do the load image thing (every time I type with any random key of the keyboard, it loads a random bullet trace on the background) I used the following code:
PImage img;
void setup() {
img = loadImage("Throne_2.jpg");
background(img);
size(1500, 900);
}
void draw() {
// (val = = )
if (keyPressed == true) {
print("ddd\n");
img = loadImage("1.png");
image(img, random (0, 1500), random (0, 900), 34, 45);
}
delay(100);
//loop ();
}
Now I need to replace the keyboard key with a button on arduino, so every time I press the button on Arduino it does the same thing which is loading a random bullet trace on top of the background image.
I have connected my Arduino and I wrote the codes for both Arduino software and Processing software but it is not working. I am not able to know how to do that. Here is the codes for both:
--------- Code for processing:
import processing.serial.*;
Serial port; // Create object from Serial class
int val = 0; // Data received from the serial port
int oldval = 0;
// int numFrames = 15;
int frame = 0;
// PImage[] images = new PImage(numFrames);
PImage img;
void setup() {
img = loadImage("Throne_2.jpg");
background(img);
size(1500, 900);
// println(Serial.list()); // print a list of all available ports
println((Object[])Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
// choose the port to which the Arduino is connected
// on PC is usually COMI, on Mac is usually tty.usbserial-XXX
}
void draw() {
if (0 < port.available()) {
val = port.read();
}
// if (port.digitalRead(7) == HIGH) {
if (val !=oldval && val == 7) {
// if (keyPressed == true) {
print("ddd\n");
img = loadImage("1.png");
image(img, random (0, 1500), random (0, 900), 34, 45);
}
delay(100);
}
--------- Code for Arduino:
int switchPin = 7; // Switch connected to pin 7
int LEDPin = 13;
void setup() {
pinMode(7, INPUT); // Set pin 0 as an input
pinMode(13, OUTPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(7) == HIGH) { // If switch is ON,
Serial.write(2); // send 2 to Processing
digitalWrite(13, HIGH);
} else { // If the switch is not ON,
Serial.write(1); // send 1 to Processing
digitalWrite(13, LOW);
}
delay(100); // Wait 100 milliseconds
}
Can you help me please?