warriorcatxx
Published

Button. Default. Code 01

This is a Default Button Code to turn on an LED when pressed.

IntermediateFull instructions provided76
Button. Default. Code 01

Things used in this project

Hardware components

LED (generic)
LED (generic)
×1
Arduino UNO
Arduino UNO
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Button
×1
Resistor 330 ohm
Resistor 330 ohm
×2
Jumper wires (generic)
Jumper wires (generic)
×7

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Button.Default.Wiring

The Button wiring:
Red: goes to POSITIVE
Blue: goes to NEGATIVE
Black: MISC

Code

Button.Default.Code

Arduino
Use this code!
/*
 
  ***Created by Lucian R***             
  All Rights Reserved. Use at own risk. 
  BUTTON DEFAULT CODE                   
 
 */

// Put your variables here:
int led = 6; // Change this to your LED's DigitalPin
int button = 2; // Change this to your button's DigitalPin
int value = 0; //Don't change this!!

void setup() {
  // Put your setup code here, to run once:
pinMode(led, OUTPUT);
// This declares the LED as an OUTPUT
pinMode(button, INPUT);
// This declares the button as an INPUT
}

void loop() {
  // Put your main code here, to run repeatedly:
value = digitalRead(button); 
// This checks and gives us the state of the button: Pressed (HIGH), Released (LOW). It's funny- "value" was an integer, now it's a boolean. LOL
if (value == HIGH) {
  // This checks if it's pressed.
  digitalWrite(led, HIGH);
  // And this turns the LED on.
}
else {
  // This checks if it's NOT pressed.
  digitalWrite(led,LOW);
  // This turns the LED off.
}
}

Credits

warriorcatxx
1 project • 1 follower

Comments