Arduino keep button state - button

How can I turn a led on when I press on a button, then turn it off when I press again that button?
This is my code:
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Right now the led is on as long as I keep the button pressed...
I added the components and the code here:
https://www.tinkercad.com/things/dT2gVL0hJVf-swanky-krunk/editel?sharecode=s6M2OOyAQCZ8cePou13PBvkByEE-qr-baUN6UwUuckA=

Use this code the onAndOff is boolean that has one of the two values (true, false) we put false in it if led is off we put true in it if led is on and we check if its true or false when we press the button if its false then turn the led on and put true in onAndOff otherwise turn off the led and put false in onAndOff
const int buttonPin = 2;
const int ledPin = 13;
bool onAndOff = false;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (onAndOff == false) {
onAndOff = true;
digitalWrite(ledPin, HIGH);
} else {
onAndOff = false;
digitalWrite(ledPin, LOW);
}
}
}

Related

Trying to make a password using an array with arduino

I am a beginner to using the arduino and I'm stuck at a certain problem. What I have is a programm that prints numbers to the console with the press of a button. What I'm trying to get is to the point that I enter 4 numbers after which it checks if it is the same as an array of numbers that I set before.
Now the problem is that I dont know how to make the programm check if the array I entered with the buttons is the same as the one I wrote before.
int b1 = 4;
int bs1 = 0;
int b2 = 2;
int bs2 = 0;
int b3 = 3;
int bs3 = 0;
int count = 0;
int correctcode[] = {2,3,3,1};
int code[4];
void setup() {
// put your setup code here, to run once:
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode (b1, INPUT);
pinMode (b2, INPUT);
pinMode (b3, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
bs1 = digitalRead(b1);
bs2 = digitalRead(b2);
bs3 = digitalRead(b3);
if (bs1 == HIGH) {
count++;
Serial.print ("1");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
if (bs2 == HIGH) {
count++;
Serial.print ("2");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
if (bs3 == HIGH) {
count++;
Serial.print ("3");
delay(500);
if (count == 4){
Serial.println ("pincode ingevoerd, checking....");
}
}
}
// Button pins ( with external pulldown resistors )
const byte b1 = 4;
const byte b2 = 2;
const byte b3 = 3;
// Signal pins
const byte greenPin = 12;
const byte redPin = 13;
byte count = 0; // 0 ... 4 counted button presses
byte correctcode[4] = {2,3,3,1};
byte code[4];
void setup() {
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode (b1, INPUT);
pinMode (b2, INPUT);
pinMode (b3, INPUT);
Serial.begin(9600);
}
void loop() {
bool bs1 = digitalRead(b1);
bool bs2 = digitalRead(b2);
bool bs3 = digitalRead(b3);
if (bs1) add(1);
else if (bs2) add(2);
else if (bs3) add(3);
}
void add( byte value ) {
Serial.print (value);
code[count++] = value;
if (count == 4) {
count=0;
if (check()) correct();
else wrong();
}
else
delay(500);
}
bool check() {
for (byte i=0; i < 4; i++) {
if (code[i] != correctcode[i]) {
return false;
}
}
return true;
}
void wrong() {
Serial.println(" -> wrong !");
digitalWrite(redPin, HIGH);
delay(1000);
digitalWrite(redPin, LOW);
}
void correct() {
Serial.println(" -> correct !");
digitalWrite(greenPin, HIGH);
delay(300);
digitalWrite(greenPin, LOW);
}
Compiled, but Untested
Don't duplicate code, but create functions. This can make code easier to read.
If you do not want an autorepeat ( after 500 ms ) for your buttons, better check for state changes (Pressed -> Released)
If multiple buttons are pressed, I added a priority. You might want to discard that state instead.
For more than 3 buttons, read about arrays :)

Arduino | Everything on the X-axis works but not on the Y-axis

I want to make a simple memory game for the Arduino. But instead of buttons I want to use a joystick to select the blinking LEDs.
I am in the early stage of the project but my Y-axis won't read and I can't figure the problem out.
//Joystick
const int X_pin = A4;
const int Y_pin = A5;
int Joy_Waarde;
//leds
const int led_Rood = 13;
const int led_Groen = 12;
const int led_Blauw = 11;
const int led_Geel = 10;
//componenten
const int buzzer = 4;
int X_Value = 0;
int Y_Value = 0;
void setup() {
pinMode (led_Rood, OUTPUT);
pinMode (led_Groen, OUTPUT);
pinMode (led_Blauw, OUTPUT);
pinMode (led_Geel, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(A0));
Serial.println("Het spel is begonnen!");
}
void loop() {
X_Value = analogRead(X_pin);
Y_Value = analogRead(Y_pin);
if (X_Value == 1023) {
digitalWrite(led_Groen, HIGH);
} else {
digitalWrite(led_Groen, LOW);
}
if (X_Value == 0) {
digitalWrite(led_Blauw, HIGH);
} else {
digitalWrite(led_Blauw, LOW);
}
if (Y_Value == 1023) {
digitalWrite(led_Geel, HIGH);
} else {
digitalWrite(led_Geel, LOW);
}
if (Y_Value == 0) {
digitalWrite(led_Rood, HIGH);
} else {
digitalWrite(led_Rood, LOW);
}
}
Hopefully you guys now something about it! Thanks in advance!
It worked, It was something with the wiring!
Thank you!

How do I make the buzzer stay on (LDR Alarm)?

I have a program in Arduino that checks an LDR sensor. If it goes over the set values it will trigger an alarm. How do I set it so once triggered it stays on until say a button push is detected to disarm it?
Code:
const int ledPin = 8;
const int buzzerPin = 4;
const int ldrPin = A0;
void setup () {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus >= 30) {
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
} else {
tone(buzzerPin, 100);
digitalWrite(ledPin, HIGH);
delay(100);
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
delay(100);
Serial.println("----------- ALARM ACTIVATED -----------");
}
}
You should use a FLAG to fire the alarm instead of using threshold directly.
if (ldrStatus >= 30) {
AlarmFlag = true; //Set alarm
}
...
if (digitalRead(pushButton) == LOW){
AlarmFlag = false; //Turn off alarm
}
...
if (AlarmFlag == true){
Serial.println("ALARM ON");
...
}

Arduino/coding noobing having issues adding something to an already working sketch

Being a Arduino and coding noob, I've been running into brick walls testing ways to make this work.
I would like LED2 to blink while LED1 is on. I'm not exactly sure which control to use and where to put it.
int b1IN = 10;
int LED1 = 11;
int b2IN = 4;
int LED2 = 3;
boolean lastButton = LOW;
boolean ledOn = false;
boolean currentButton = LOW;
void setup() {
pinMode(b1IN, INPUT);
pinMode(LED1, OUTPUT);
pinMode(b2IN, INPUT);
pinMode(LED2, OUTPUT);
}
boolean debounce(boolean last){
boolean current = digitalRead(b2IN);
if (last != current){
delay (15);
current = digitalRead(b2IN);
}
return current;
}
void loop() {
currentButton = debounce(lastButton);
if (digitalRead(b1IN) == HIGH) {
digitalWrite(LED1, HIGH);
delay(20000);
digitalWrite(LED1, LOW);
}
if (lastButton == LOW && currentButton == HIGH) {
ledOn = !ledOn;
}
lastButton = currentButton;
digitalWrite(LED2, ledOn);
}

Arduino simple button activated light

I keep getting an error when I run this code any solutions?
{
pinMode(button2pin, INPUT, 3);
button1State= digitalRead(button1Pin 2);
}
if (button1State == LOW)
{
}
This code presumes that you have connected one side of your switch to digital pin 3 and the other side to ground and that your button is normally open.
const int button2pin = 3;
int button2State = 0;
void setup(){
pinMode(button2pin, INPUT_PULLUP);
Serial.begin(9600); // for debug only
}
void loop(){
button2State = digitalRead(button2pin);
if(button2State == LOW){
Serial.print("Button 2 pressed"); // for debug only
}
}
This project turns on the light when the button is pressed. Maybe can help you with your problem.
int buttonPin = 2;
int ledPin = 5;
//This var read the state of Buttonpin
int buttonState = 0;
void setup() {
// set the pin as output
pinMode(ledPin , OUTPUT);
// set the pin as input
pinMode(buttonPin , INPUT);
}
void loop(){
// read the button state
buttonState = digitalRead(buttonPin );
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}

Resources