Jump to content

How to make IF statement for if something changes in Arduino

Clanscorpia

Hello,

I'm trying to make an Arduino program where a serial message is displayed and when something comes in but I dont know what I would do for the If statment to show what happens if the value of a button changes. if ( I dont know what to put here )

{

}

What do I put? Just the variable and then =! ?

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

put an if statement in a loop always checking the current condition compared to the previous one

LIBERATOR: Core i5 6400 @ 2.7GHz | GeForce GTX 670 2 GB | HyperX Fury 8GB DDR4 @ 2133 MHz | 250 GB Samsung 850 Evo SSD
My new build; Liberator, Check it out

The more friendly we are, the more helpful we are!

 

MY OLD BUILD (AKA PREBUILT MONSTROSITY)
INSANITY: AMD Athlon II X2 @ 3.0 GHz | Geforce GT 720 2 GB | 4 GB DDR3 @ 1333 MHz | 120 GB Silicon Power SSD | 500 GB Hitachi HDD

Link to comment
Share on other sites

Link to post
Share on other sites

somthing like this (sorry if the code isnt optimal)

_______________________________________

 

bool buttonold = digitalRead();

bool buttonnew = digitalRead();

 

void loop(){

buttonnew = digitalRead();

if(buttonnew != buttonold){

   Serial.println(message);

   buttonold = digitalRead();

}

}

Altair - Firestrike: http://www.3dmark.com/3dm/13945459

CPU:  i7-4790 @ 3.6 GHz Motherboard: Gigabyte B85M-DS3H-A RAM: 16GB @ 1600MHz CL11 GPU: XFX RX 470 RS Storage: ADATA SP550 240GB | WD Blue 1TB | Toshiba 2TB PSU: EVGA B2 750W Case: Phanteks Enthoo Pro Fans: Phanteks PH-F200SP (Front) | Phanteks PH-F140SP (Rear) | Noctua NF-A15 (Top)

Mouse: Logitech G502 | Keyboard: Corsair K70 MX Brown | Audio: Sennheiser HD 558

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, DrM said:

somthing like this (sorry if the code isnt optimal)

_______________________________________

 

bool buttonold = digitalRead();

bool buttonnew = digitalRead();

 

void loop(){

buttonnew = digitalRead();

if(buttonnew != buttonold){

   Serial.println(message);

   buttonold = digitalRead();

}

}

 

2 minutes ago, Philcat101 said:

put an if statement in a loop always checking the current condition compared to the previous one

Exactly what I was saying, good idea. OP read this

LIBERATOR: Core i5 6400 @ 2.7GHz | GeForce GTX 670 2 GB | HyperX Fury 8GB DDR4 @ 2133 MHz | 250 GB Samsung 850 Evo SSD
My new build; Liberator, Check it out

The more friendly we are, the more helpful we are!

 

MY OLD BUILD (AKA PREBUILT MONSTROSITY)
INSANITY: AMD Athlon II X2 @ 3.0 GHz | Geforce GT 720 2 GB | 4 GB DDR3 @ 1333 MHz | 120 GB Silicon Power SSD | 500 GB Hitachi HDD

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, DrM said:

somthing like this (sorry if the code isnt optimal)

_______________________________________

 

bool buttonold = digitalRead();

bool buttonnew = digitalRead();

 

void loop(){

buttonnew = digitalRead();

if(buttonnew != buttonold){

   Serial.println(message);

   buttonold = digitalRead();

}

}

Damn, I didn't want to write that kind of statement, they take ages

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, DrM said:

somthing like this (sorry if the code isnt optimal)

_______________________________________

 

bool buttonold = digitalRead();

bool buttonnew = digitalRead();

 

void loop(){

buttonnew = digitalRead();

if(buttonnew != buttonold){

   Serial.println(message);

   buttonold = digitalRead();

}

}

Wait I see what you did there. Last time I did something like that is was much different. Thank you very much. It's going to be used for a littleBits  project to test wireless latency and if you want I can link you to the final outcome

He who asks is stupid for 5 minutes. He who does not ask, remains stupid. -Chinese proverb. 

Those who know much are aware that they know little. - Slick roasting me

Spoiler

AXIOM

CPU- Intel i5-6500 GPU- EVGA 1060 6GB Motherboard- Gigabyte GA-H170-D3H RAM- 8GB HyperX DDR4-2133 PSU- EVGA GQ 650w HDD- OEM 750GB Seagate Case- NZXT S340 Mouse- Logitech Gaming g402 Keyboard-  Azio MGK1 Headset- HyperX Cloud Core

Offical first poster LTT V2.0

 

Link to comment
Share on other sites

Link to post
Share on other sites

Constant polling of a pin is not optimal, you need to use ISR on a pin that executes a function of yours, that's the best way to implement such a thing.  Refer here.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×