1. Chapter LED

We will still start from Blink LED in this chapter, and also learn the usage of some commonly used functions of Processing Software.

1.2. Project MouseLED

In this project, we will use the mouse to control the state of LED.

The components and circuits of this project are the same as the previous section.

1.2.1. Sketch

1.2.1.1. Sketch MouseLED

First, observe the result after running the sketch, and then learn the code in detail.

  1. Use Processing to open the file Sketch_01_2_1_MouseLED.

$ processing ~/Freenove_Kit/Processing/Sketches/Sketch_01_2_1_MouseLED/Sketch_01_2_1_MouseLED.pde
  1. Click on “RUN” to run the code.

After the program is executed, the LED is in OFF-state, and background color of Display window is gray. Click the grey area of the Display Window with the mouse, LED is turned ON and Display window background color becomes red. Click on the Display Window again, the LED is turned OFF and the background color becomes gray, as shown below.

../../../_images/LED_state_grey.png

The following is program code:

 1/*****************************************************
 2 * Filename    : Sketch_01_2_1_MouseLED
 3 * Description : Use the mouse to control the LED ON OFF
 4 * auther      : www.freenove.com
 5 * modification: 2024/09/04
 6 *****************************************************/
 7import freenove.processing.io.*;
 8
 9int ledPin = 17;
10boolean ledState = false;
11void setup() {
12  size(100, 100);
13  GPIO.pinMode(ledPin, GPIO.OUTPUT);
14  background(102);
15}
16
17void draw() {
18  if (ledState) {
19    GPIO.digitalWrite(ledPin, GPIO.HIGH);
20    background(255,0,0);
21  } else {
22    GPIO.digitalWrite(ledPin, GPIO.LOW);
23    background(102);
24  } 
25}
26
27void mouseClicked() { //if the mouse Clicked
28  ledState = !ledState;  //Change the led State
29}

The function mouseClicked() in this code is used to capture the mouse click events. Once the mouse is clicked, the function will be executed. We can change the state of the variable “ledState” in this function to realize controlling LED by clicking on the mouse.

1void mouseClicked() { //if the mouse Clicked
2  ledState = !ledState;  //Change the led State
3}

1.2.2. Freenove Car, Robot and other products for Raspberry Pi

We also have car and robot kits for Raspberry Pi. You can visit our website for details.

https://www.amazon.com/freenove

FNK0043–Freenove 4WD Smart Car Kit for Raspberry Pi

FNK0050–Freenove Robot Dog Kit for Raspberry Pi

FNK0052–Freenove_Big_Hexapod_Robot_Kit_for_Raspberry_Pi