Arduino simple button activated light - arduino

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);
}
}

Related

Arduino keep button state

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);
}
}
}

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");
...
}

Power button IRremote

I am creating 3 LEDs that will light up by a remote. I am able to light up the LEDs individually but I need the power button to shut off all of the LEDs. How can I create a 4th case to turn off all LEDs?
#include <IRremote.h>
int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838
int led1 = 2;
int led2 = 4;
int led3 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 12495 // code received from button A
#define code2 6375 // code received from button B
#define code3 31365 // code received from button C
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}
break;
case code2:
if(itsONled[2] == 1) {
digitalWrite(led2, LOW);
itsONled[2] = 0;
} else {
digitalWrite(led2, HIGH);
itsONled[2] = 1;
}
break;
case code3:
if(itsONled[3] == 1) {
digitalWrite(led3, LOW);
itsONled[3] = 0;
} else {
digitalWrite(led3, HIGH);
itsONled[3] = 1;
}
break;
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}

Arduino Combination Button?

I am trying to set up an Arduino Uno plus a RelayShield along with 6 push buttons(standard), and 3 LED's(red, yellow, green). Now what I have code to do is to take in a button push combination that is 6 pushes long(order only matters for what is set in the array). I have everything wired up to a breadboard and all that other jazz. My problem is, none of the LED's are working(I followed detailed instructions from instructables - minus the code), and it doesn't matter how many button pushes there are, only button 3 triggers the relay.... Now, for simplicities sake, I didn't wire up all 6 buttons, and I just shortened the array down to 3 and adjusted accordingly(I didn't want to hook up 6 buttons right now). Can someone verify this code for me? Or tell me what is wrong with it? Thanks in advance!
//button pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
const int grnLed = 9;
const int redLed = 10;
const int yellowLed = 11;
const int openRelay = 8;
// how long is our code, kinda personal
const int codelen = 3;
//pin code values must match button pin values
char PIN[codelen] = {
'1', '2', '3'
};
// attempted combo
char attempt[codelen] = {
'0', '0', '0'
};
// attempt count
int z = 0;
void setup() {
// you've been setup
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(openRelay, OUTPUT);
// set pullup resistor for buttons
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
// set openRelay state to open or closed
digitalWrite(openRelay, LOW);
}
void correctPIN()
{
pulseLED(grnLed, 3000);
digitalWrite(openRelay, HIGH);
delay(2000);
digitalWrite(openRelay, LOW);
delay(2000);
z = 0;
}
void incorrectPIN()
{
pulseLED(redLed, 3000);
z = 0;
}
void checkPIN()
{
int correct = 0;
int i;
for (i = 0; i < codelen; i++)
{
if (attempt[i] == PIN[i])
{
correct++;
}
}
if (correct == codelen)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz = 0; zz < codelen; zz++)
{
attempt[zz] = '0';
}
}
void checkButton(int button){
if (digitalRead(button) == LOW)
{
while (digitalRead(button) == LOW) { } // do nothing
//convert int to string for tracking/compare
char buttStr = button + '0';
attempt[z] = buttStr;
z++;
//light up led so we know btn press worked
pulseLED(yellowLed, 500);
}
}
void pulseLED(int ledpin, int msec) {
digitalWrite(ledpin, HIGH);
delay(msec);
digitalWrite(ledpin, LOW);
}
void loop() {
// check buttons
checkButton(button1);
checkButton(button2);
checkButton(button3);
checkButton(button4);
checkButton(button5);
checkButton(button6);
//if number of buttons pressed, z, matches code/pin length then check
if (z >= codelen)
{
checkPIN();
}
}

Arduino program that allows me to change state through comm port?

new here! been recommended many times to come here for help so here I am.
I'm supposed to write a program that allows me to change the rate of a blinking LED light through the comm port. I'm sure this is easy but I've honestly got no clue as I am behind in this class.
anything would help really, i honestly want to learn how to do this, not just come here and get the answer.
thanks in advanced!
// global variables
#include <EEPROM.h>
unsigned long ms_runtime;
int state;
// possible values 0 -> 1 -> 2 -> 3 -> 0
int one_ms_timer;
// define all timers as unsigned long (they are incremented every 100ms = 0.1s)
unsigned long timer1;
unsigned long button_dbnc_tmr = 0;
// timer1 is used for blinking LED
const int LED1 = 13;
// function prototypes
void read_memory(void);
void update_memory(void);
void comm_control(void);
void led_control(void);
void turnoff(void);
void flash_1s(void);
void flash_2s(void);
void flash_3s(void);
void timers(void);
void setup()
{
read_memory();
pinMode(LED1, OUTPUT);
Serial.begin(9600);
//initialize uart
}
void loop()
{
static bool allow_change;
static int counter;
timers();
comm_control();
led_control();
}
void led_control()
{
switch (state)
{
case 0:
turnoff();
break;
case 1:
flash_1s();
break;
case 2:
flash_2s();
break;
case 3:
flash_3s();
break;
}
}
void turnoff()
{
digitalWrite(LED1, LOW);
}
void flash_1s()
{
if (timer1 < 10)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 20)
timer1 = 0;
}
}
void flash_2s()
{
if (timer1<20)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 30)
timer1 = 0;
}
}
void flash_3s()
{
if (timer1<30)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 40)
timer1 = 0;
}
}
void read_memory()
{
timer1 = EEPROM.read(one_ms_timer);
timer1++;
EEPROM.write(one_ms_timer, timer1);
Serial.begin(9600);
}
void update_memory()
{
EEPROM.update(timer1, one_ms_timer);
}
void comm_control(void);
{
char comm_reveier = serial_read;
if (
)
}
void timers(void)
{
if (millis() > (ms_runtime + 1))
{
ms_runtime = ms_runtime + 1;
one_ms_timer++;
}
else if (ms_runtime > millis())
ms_runtime = millis();
if (one_ms_timer > 99) //every 100 ms
{
one_ms_timer = 0;
button_dbnc_tmr++;
timer1++;
}
}
Load the Standard Firmata library on your Arduino board. Then use a library of your choice to build your comm prog. An overview of these can be found here.
Assuming you are using the Arduino IDE, this code sample should give you the general idea of what you need to do:
// pins for the LEDs:
const int ledPin = 13;
// Default blink rate
int rate = 1000;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(ledPin, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int rate = Serial.parseInt();
}
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(rate); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(rate); // wait for a second
}

Resources