Time to push buttons!
In this part, I'm going to show you how we can move objects within the screen and debug some pieces of code. I guess you have read and already know something about C programming language, if not – don't worry and try to analyze what I do.
Firstly, we need to initialize I/O peripherals: buttons, speaker, LEDs, display. Set enabled buttons pins as Inputs, speaker and LEDs pins to Output. Display initialization will be covered later. Learn it from examples!
// Hardware setup
pinMode (UP, INPUT_PULLUP);
pinMode (DOWN, INPUT_PULLUP);
pinMode (LEFT, INPUT_PULLUP);
pinMode (RIGHT, INPUT_PULLUP);
pinMode (A, INPUT_PULLUP);
pinMode (B, INPUT_PULLUP);
pinMode (SPEAKER, OUTPUT);
pinMode (LED, OUTPUT);
pinMode (ONBOARD_LED, OUTPUT);
digitalWrite(SPEAKER, LOW);
There are several basic methods to read the pin of Arduino. As we interested only in two values of a button, pressed or not, we use a digitalRead() function which can return High either Low state.
We can read button state like this:
if (digitalRead(8) == LOW) { // 8 is a number of pin where button is connected.
digitalWrite(LED, HIGH); // Turn LED on
} else {
digitalWrite(LED, LOW); // Turn LED off
}
We compare the state of the button with LOW, because we use the PULLUP resistors, it made buttons to be in the HIGH state when they are not pushed. You can learn more details about it on the official Arduino website.
The same things:
if (digitalRead(8) == false) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
Also, you can read in this way:
if ( !digitalRead(8) ) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
As you can see I wrote "8" This is the number of a pin where a button is connected. Obviously, this is not convenient for humans. We may forget what pins got buttons, where is speaker connected etc.
Let's define names.
// Hardware Init
#define UP 8
#define DOWN 10
#define LEFT 9
#define RIGHT 5
#define A A0
#define B A1
#define SPEAKER 3
// Debugging LEDs
#define LED 2
#define ONBOARD_LED 13
Time to debug buttons! Are they properly connected and respond? Instead of writing a huge amount of the same type of code for each button I wrote testButton() function.
Try to analyze (copy and paste into Arduino IDE) and run the code.
CODE:
---------------------------------------------------------------------
/*
Game Starter Project 15.02.2017
Board: Arduino Nano (ATmega328), OLED Display: SSD1306 i2c
Author: Aleksey M.
http://eekit.blogspot.com
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED Display Init
#define SSD1306_LCDHEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
// Hardware Init
#define UP 8
#define DOWN 10
#define LEFT 9
#define RIGHT 5
#define A A0
#define B A1
#define SPEAKER 3
// Debugging LEDs
#define LED 2
#define ONBOARD_LED 13
void setup() {
// Hardware setup
pinMode (UP, INPUT_PULLUP);
pinMode (DOWN, INPUT_PULLUP);
pinMode (LEFT, INPUT_PULLUP);
pinMode (RIGHT, INPUT_PULLUP);
pinMode (A, INPUT_PULLUP);
pinMode (B, INPUT_PULLUP);
pinMode (SPEAKER, OUTPUT);
pinMode (LED, OUTPUT);
pinMode (ONBOARD_LED, OUTPUT);
digitalWrite(SPEAKER, LOW);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// initialize with the I2C addr 0x3D (for the 128x64)
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
display.display();
delay(500);
// Clear the buffer.
display.clearDisplay();
// Invert Display.
//display.invertDisplay(true);
display.setTextSize(1);
display.setTextColor(WHITE);
}
void loop() {
testButton(UP);
testButton(DOWN);
testButton(LEFT);
testButton(RIGHT);
testButton(A);
testButton(B);
// Turn LED of
digitalWrite(LED, LOW);
// Clear display
display.clearDisplay();
display.display();
}
void testButton(int button) {
while (digitalRead(button) == LOW) {
digitalWrite(LED, HIGH);
// Print on the display
display.setCursor(0, 0);
display.println("Works!");
display.display();
}
}
Here is readButton() function, that we will use in the next part of Arduino game programming tutorial.
In this part, I'm going to show you how we can move objects within the screen and debug some pieces of code. I guess you have read and already know something about C programming language, if not – don't worry and try to analyze what I do.
Firstly, we need to initialize I/O peripherals: buttons, speaker, LEDs, display. Set enabled buttons pins as Inputs, speaker and LEDs pins to Output. Display initialization will be covered later. Learn it from examples!
// Hardware setup
pinMode (UP, INPUT_PULLUP);
pinMode (DOWN, INPUT_PULLUP);
pinMode (LEFT, INPUT_PULLUP);
pinMode (RIGHT, INPUT_PULLUP);
pinMode (A, INPUT_PULLUP);
pinMode (B, INPUT_PULLUP);
pinMode (SPEAKER, OUTPUT);
pinMode (LED, OUTPUT);
pinMode (ONBOARD_LED, OUTPUT);
digitalWrite(SPEAKER, LOW);
There are several basic methods to read the pin of Arduino. As we interested only in two values of a button, pressed or not, we use a digitalRead() function which can return High either Low state.
An analogRead() function can read analog compatible pins like A0, A1, A2... and return a value in a range of 0 to 1023 for 10-bit ADC. We do not use this function in the project.
We can read button state like this:
if (digitalRead(8) == LOW) { // 8 is a number of pin where button is connected.
digitalWrite(LED, HIGH); // Turn LED on
} else {
digitalWrite(LED, LOW); // Turn LED off
}
We compare the state of the button with LOW, because we use the PULLUP resistors, it made buttons to be in the HIGH state when they are not pushed. You can learn more details about it on the official Arduino website.
The same things:
if (digitalRead(8) == false) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
Also, you can read in this way:
if ( !digitalRead(8) ) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
As you can see I wrote "8" This is the number of a pin where a button is connected. Obviously, this is not convenient for humans. We may forget what pins got buttons, where is speaker connected etc.
Let's define names.
// Hardware Init
#define UP 8
#define DOWN 10
#define LEFT 9
#define RIGHT 5
#define A A0
#define B A1
#define SPEAKER 3
// Debugging LEDs
#define LED 2
#define ONBOARD_LED 13
Time to debug buttons! Are they properly connected and respond? Instead of writing a huge amount of the same type of code for each button I wrote testButton() function.
Try to analyze (copy and paste into Arduino IDE) and run the code.
CODE:
---------------------------------------------------------------------
/*
Game Starter Project 15.02.2017
Board: Arduino Nano (ATmega328), OLED Display: SSD1306 i2c
Author: Aleksey M.
http://eekit.blogspot.com
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED Display Init
#define SSD1306_LCDHEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
// Hardware Init
#define UP 8
#define DOWN 10
#define LEFT 9
#define RIGHT 5
#define A A0
#define B A1
#define SPEAKER 3
// Debugging LEDs
#define LED 2
#define ONBOARD_LED 13
void setup() {
// Hardware setup
pinMode (UP, INPUT_PULLUP);
pinMode (DOWN, INPUT_PULLUP);
pinMode (LEFT, INPUT_PULLUP);
pinMode (RIGHT, INPUT_PULLUP);
pinMode (A, INPUT_PULLUP);
pinMode (B, INPUT_PULLUP);
pinMode (SPEAKER, OUTPUT);
pinMode (LED, OUTPUT);
pinMode (ONBOARD_LED, OUTPUT);
digitalWrite(SPEAKER, LOW);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// initialize with the I2C addr 0x3D (for the 128x64)
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
display.display();
delay(500);
// Clear the buffer.
display.clearDisplay();
// Invert Display.
//display.invertDisplay(true);
display.setTextSize(1);
display.setTextColor(WHITE);
}
void loop() {
testButton(UP);
testButton(DOWN);
testButton(LEFT);
testButton(RIGHT);
testButton(A);
testButton(B);
// Turn LED of
digitalWrite(LED, LOW);
// Clear display
display.clearDisplay();
display.display();
}
void testButton(int button) {
while (digitalRead(button) == LOW) {
digitalWrite(LED, HIGH);
// Print on the display
display.setCursor(0, 0);
display.println("Works!");
display.display();
}
}
Here is readButton() function, that we will use in the next part of Arduino game programming tutorial.
bool readButton(int button) {
if (digitalRead(button) == LOW) {
// Debounce. Mechanical buttons generate a flickering signal, so let button become to LOW.
delay(20);
if (digitalRead(button) == LOW) {
return true;
}
}
else {
return false;
}
}
Buttons are ready to use!
What have we learned? We have learned: pins initialization and definitions, buttons state readings, the basic debugging method, have a light look on the functions.
What have we learned? We have learned: pins initialization and definitions, buttons state readings, the basic debugging method, have a light look on the functions.
No comments:
Post a Comment