Website powered by

7/12/21 - Electronics - Leonardo

General / 11 January 2022

7/12/21

The Arduino Leonardo arrived in the mail today. To ensure everything was working I uploaded an example LED blinking program in the Leonardo. (pictured below) The LED above the Green ON LED, blinks on and off. This tells me the IDE recognizes the board and I can successfully move forward. 

I then removed the off brand board from my breadboard and replaced it with the Leonardo Arduino. The circuit runs the same as it did before.

I uploaded the text code I made last week into the Leonardo and it worked. I was able to print out the letter "A" when I pushed the button. I then replaced "Keyboard.Print ("A") " with "Keyboard.Print ("Hello World") ". I updated the program and went to google. When I clicked the google search bar and pressed the button the words "Hello World" appears. 

(below is the code)

Next I reopened the p5.js program I made last week. The program features a rectangle when any keyboard button is pressed the rectangle will change from "white" to black" and vice versa. I am testing to see if I can create a physical button which can trigger events within a computer program. (such as Unity of p5.js).

Link to p5.js editor (code)

Link to Video

It works! However, small problem. When the physical button is held, the square continually changes back and forth from black to white. when the button is held I want the state to not change again until the button is pressed again. One solution, I changed the code in the p5.js editor. Instead of any button changing the rect color only specific keyboard buttons will.

       "A" will change rect to "white"

       "Z" will change rect to "black"

I then changed the code on the Arduino. Basically, the code remembers the previous buttons state and then spits out a specific keyboard letter based on the value. So, if the button value is "0" then "A" will be printed and if the button value is "1" then "Z" will be printed. with both sets of codes altered, I am now able to press and hold the physical button down for as long as I like, and the rectangle will only change states once.

Reflection

I believe I have solved the "problem" I was having, however, I question if this will also work in other cases? such as inside of Unity? The digital ON and OFF buttons are coded as two different buttons ("A" and "Z")... I feel like the letters need to be the same. This new system feels like I am turning the lights on with one switch and turning the lights off with another. I don't like that I have to use two different "switches" to toggle between two states using a psychical button. it seems inefficient.