ArnauMGerardP
Published © GPL3+

LEDs and Buzzer Controlled by Joystick

In this project, we have created a set of leds and a buzzer, that are controlled by a joystick.

IntermediateProtip10,045
LEDs and Buzzer Controlled by Joystick

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Analog joystick (Generic)
×1
LEDs
×1
Buzzer
Buzzer
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

HardWare

The BUZZER goes to the pin "4"

Code

SoftWare

C/C++
const int buzzer = 4;
const int ledleft = 13;
const int ledright = 8;
const int ledbehind = 7;
const int ledfront = 12;
const int SW_pin = 2;
const int X_pin = 0;
const int Y_pin = 1;

void setup() {
  
  pinMode(buzzer, OUTPUT);
  pinMode(ledleft, OUTPUT);
  pinMode(ledright, OUTPUT);
  pinMode(ledbehind, OUTPUT);
  pinMode(ledfront, OUTPUT);
 
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);
}

void loop() {
  
  int position_x = analogRead(X_pin);
  int position_y = analogRead(Y_pin);
  int switch_status = digitalRead(SW_pin);
  
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(1);

 if (switch_status == 0){
  digitalWrite (buzzer, HIGH);
 }
 else {
  digitalWrite (buzzer, LOW);
 }
 if (position_x <= 500) {
  digitalWrite (ledbehind, HIGH); 
 }
 else {
  digitalWrite (ledbehind, LOW);
 }
 if (position_x >= 520) {
  digitalWrite (ledfront, HIGH);
 }
 else {
  digitalWrite (ledfront, LOW); 
 }
if (position_y >= 520) {
  digitalWrite (ledright, HIGH);
}
else {
  digitalWrite (ledright, LOW);
}
 if (position_y <=500) {
  digitalWrite (ledleft, HIGH);
 }
 else {
  digitalWrite (ledleft, LOW);
 }
}

Credits

ArnauM

ArnauM

0 projects • 3 followers
GerardP

GerardP

0 projects • 2 followers

Comments