Unit 18: Directional Control
Code for Sketch One:
#include
ezButton button(2); // Create an ezButton object for the joystick button
// LED pin definitions
const int northLED = 3;
const int eastLED = 4;
const int southLED = 6;
const int westLED = 5;
// Joystick axis
const int joystickX = A0; // Horizontal position
const int joystickY = A1; // Vertical position
// Threshold for joystick movement
const int threshold = 200; // Adjust this value based on sensitivity
// Sequence tracking
int currentStep = 0; // To track the current step in the sequence
void setup() {
Serial.begin(9600); // Start serial communication for debugging
// Set LED pins as output
pinMode(northLED, OUTPUT);
pinMode(eastLED, OUTPUT);
pinMode(southLED, OUTPUT);
pinMode(westLED, OUTPUT);
// Initialize the button
button.setDebounceTime(50); // Debounce time for the button
}
void loop() {
button.loop(); // Update button state
// Read joystick positions
int xPos = analogRead(joystickX);
int yPos = analogRead(joystickY);
// Print joystick positions for debugging
Serial.print("X Position: ");
Serial.print(xPos);
Serial.print(" | Y Position: ");
Serial.println(yPos);
// Check joystick movements in order
if (currentStep == 0 && yPos < 512 - threshold) { // Move to North
digitalWrite(northLED, HIGH);
currentStep = 1; // Move to next step in sequence
}
else if (currentStep == 1 && xPos > 512 + threshold) { // Move to East
digitalWrite(eastLED, HIGH);
currentStep = 2; // Move to next step in sequence
}
else if (currentStep == 2 && xPos < 512 - threshold) { // Move to West
digitalWrite(westLED, HIGH);
currentStep = 3; // Move to next step in sequence
}
else if (currentStep == 3 && yPos > 512 + threshold) { // Move to South
digitalWrite(southLED, HIGH);
currentStep = 4; // Sequence complete
}
// If the sequence is complete, reset all LEDs and steps
if (currentStep == 4) {
delay(1000); // Keep the lights on for a moment
digitalWrite(northLED, LOW);
digitalWrite(eastLED, LOW);
digitalWrite(westLED, LOW);
digitalWrite(southLED, LOW);
currentStep = 0; // Reset sequence
}
// Check if the joystick button is pressed
if (button.isPressed()) {
Serial.println("Button Pressed!");
}
delay(100); // Delay for a short time
}
Code for Sketch Two (Serial output):
#include
ezButton button(2); // Create an ezButton object for the joystick button
// LED pin definitions
const int northLED = 3;
const int eastLED = 4;
const int southLED = 6;
const int westLED = 5;
// Joystick axis
const int joystickX = A0; // Horizontal position
const int joystickY = A1; // Vertical position
// Threshold for joystick movement
const int threshold = 200; // Adjust this value based on sensitivity
// Sequence tracking
int currentStep = 0; // To track the current step in the sequence
void setup() {
Serial.begin(9600); // Start serial communication for debugging
// Set LED pins as output
pinMode(northLED, OUTPUT);
pinMode(eastLED, OUTPUT);
pinMode(southLED, OUTPUT);
pinMode(westLED, OUTPUT);
// Initialize the button
button.setDebounceTime(50); // Debounce time for the button
}
void loop() {
button.loop(); // Update button state
// Read joystick positions
int xPos = analogRead(joystickX);
int yPos = analogRead(joystickY);
// Print joystick positions for debugging
Serial.print("X Position: ");
Serial.print(xPos);
Serial.print(" | Y Position: ");
Serial.println(yPos);
// Check joystick movements in order
if (currentStep == 0 && yPos < 512 - threshold) { // Move to North
digitalWrite(northLED, HIGH);
Serial.println("Step 1: Moved North");
currentStep = 1; // Move to next step in sequence
}
else if (currentStep == 1 && xPos > 512 + threshold) { // Move to East
digitalWrite(eastLED, HIGH);
Serial.println("Step 2: Moved East");
currentStep = 2; // Move to next step in sequence
}
else if (currentStep == 2 && xPos < 512 - threshold) { // Move to West
digitalWrite(westLED, HIGH);
Serial.println("Step 3: Moved West");
currentStep = 3; // Move to next step in sequence
}
else if (currentStep == 3 && yPos > 512 + threshold) { // Move to South
digitalWrite(southLED, HIGH);
Serial.println("Step 4: Moved South - Sequence Complete!");
currentStep = 4; // Sequence complete
}
// If the sequence is complete, reset all LEDs and steps
if (currentStep == 4) {
delay(1000); // Keep the lights on for a moment
Serial.println("Resetting sequence...");
digitalWrite(northLED, LOW);
digitalWrite(eastLED, LOW);
digitalWrite(westLED, LOW);
digitalWrite(southLED, LOW);
currentStep = 0; // Reset sequence
}
// Check if the joystick button is pressed
if (button.isPressed()) {
Serial.println("Button Pressed!");
}
delay(100); // Delay for a short time
}