Arduino - How to show a table when you push a button? - button

I want to show a table with certain values when I push a button in arduino-uno.
I have different buttons that are use to read a password, but I need the pinMode(8, INPUT); to show the records.
This code to show the table when I was using the keyboard on the monitor, but I need to change this to use the pinMode(8, INPUT); instead the button 0 of the keyboard.
if (Serial.available()>0) {
incomingByte = Serial.readStringUntil('\n');
Serial.println(incomingByte);
int num=incomingByte.toInt();
if (incomingByte=="0"){
for (int x=0; x<tam;x++){
}
}
Here is more code:
//Pushbutton
Serial.begin(9600);
pinMode(2,INPUT); //button 1
pinMode(3,INPUT); //button 2
pinMode(4,INPUT); //button 3
pinMode(5,INPUT); //button 4
pinMode(6, OUTPUT); //LED RED
pinMode(7, OUTPUT); //LED GREEN
pinMode(8, INPUT); //button to show the tabl
pinMode(11, OUTPUT); //buzzer
}
void loop()
{
pushButton(2,1,0);
pushButton(3,2,1);
pushButton(4,3,2);
pushButton(5,4,3);
if (Serial.available()>0) {
incomingByte = Serial.readStringUntil('\n');
Serial.println(incomingByte);
int num=incomingByte.toInt();
if (incomingByte=="0"){
for (int x=0; x<tam;x++){
}
}
//Funcion pushButton
String pushButton(int pin, int valor, int index)
{
int buttonState=digitalRead(pin);
int state;
String valor_s=String(valor);
state=stateS[index];
switch(state)
{
case 0:
if (buttonState==1){
state=1;
num=num+valor_s;
if (num.length()==4){
Serial.println(num);
for (int i = 0; i < 10; i = i + 1) {
if (client[i]==num){
count[i]=count[i]+1;
found=true;
x=i;
Serial.println("found");
break;
}
} //fin for
//* more code *//
}
} //case 0
break;
case 1:
if (buttonState==0){
state=0;
}
break;
} //switch
stateS[index]=state;
return num;
} //Fin String pushButton

Related

Implementing the function interrupt with if statements

I want to implement the function interrupt () but I don't know exactly how..In this case there is 2 for loops which can be seen in the code:I want whenever one of the 2 buttons is pressed the process inside the loop to be interrupted immediately:
void loop() {
int brightButton = digitalRead(K1);
int ldrStatus = analogRead(ldrPin);
if (brightButton == LOW && ldrStatus >= 200)
{
for (int i = 0; i < 10; i++)
{
digitalWrite(greenLed, HIGH);
tone(buzzer,400);
delay(500);
noTone(buzzer);
delay(500);
}
}
else {
digitalWrite(greenLed, LOW);
}
int tempButton = digitalRead(K2);
int valNTC = analogRead(NTC);
if (tempButton == LOW && valNTC > 512)
{
for (int i = 0; i <10; i++)
{
digitalWrite(redLed, HIGH);
tone(buzzer,450);
delay(300);
noTone(buzzer);
delay(1000);
}
}
else {
digitalWrite(redLed, LOW);
}
}
Example code from the Arduino manual:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Note that this will interrupt the for loop and return to it once the interrupt service routine is finished.
If you want to abort the for loop check the pin state in every loop cycle and break if you want to leave the for loop or return if you want to leave loop().
Of course this is not "immediately".

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

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 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