A lot of coding is basically telling either the compiler what to do or the arduino running a script what to do
"I have 4 micro switchs."
ok I need to tell the compiler that I have 4 switchs and I don't want them to be changed from the pins I assign to them. The compiler understands English so we can use words like switchB so it knows that that anytime we say switchB it means 26. This also allows us to use really long names that makes reading the code easier with no cost to the memory
#define switchA 22
#define switchB 26
#define switchC 30
#define switchD 34
next I need to set the buttons in set up to be inputs
#define switchA 22
#define switchB 26
#define switchC 30
#define switchD 34
void setup() {
// put your setup code here, to run once:
pinMode (switchA, INPUT);
pinMode (switchB, INPUT);
pinMode (switchC, INPUT);
pinMode (switchD, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
}
now I want to press button A and get a result of 1, b2, c4, d8
my highest number is going to be 8 so I need to tell the compiler that I need a place to store this number. Im also going to tell the compiler that when it starts I want the number to be 0.
research what the max number for a signed byte, int , long is.
also research what signed and unsigned means when they over flow.
then look at what a float is as you will need that at some point.
#define switchA 22
#define switchB 26
#define switchC 30// could have used a const instead of define
#define switchD 34
byte number=0;//global......research what that means
void setup() {
// put your setup code here, to run once:
pinMode (switchA, INPUT);
pinMode (switchB, INPUT);
pinMode (switchC, INPUT);
pinMode (switchD, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
}
The arduino will not look at the inputs unless I tell it to so I have to either tell it to look at certain times or I can have the arduino check the inputs every loop. Its easier to check every loop.
To do this im going to have to make a memory address so the arduino remembers what state the button was in so I can test it later
#define switchA 22
#define switchB 26
#define switchC 30
#define switchD 34
byte number = 0;
void setup() {
// put your setup code here, to run once:
pinMode (switchA, INPUT);
pinMode (switchB, INPUT);
pinMode (switchC, INPUT);
pinMode (switchD, INPUT);
}
void loop() {
// put your main code here, to run repeatedly
byte inputSwitchA = digitalRead (switchA);
byte inputSwitchB = digitalRead (switchB);
byte inputSwitchC = digitalRead (switchC);
byte inputSwitchD = digitalRead (switchD);
}
Now all I need to do is assign a value to "number" when a switch is pressed
// put your main code here, to run repeatedly
byte inputSwitchA = digitalRead (switchA);
byte inputSwitchB = digitalRead (switchB);
byte inputSwitchC = digitalRead (switchC);
byte inputSwitchD = digitalRead (switchD);
if (inputSwitchA == HIGH) {
number = 1;
}
if (inputSwitchB == HIGH) {
number = 2;
}
if (inputSwitchC == HIGH) {
number = 4;
}
if (inputSwitchD == HIGH) {
number = 8;
}
}
now after every change im using alt T to check the code then compiling it to see if ive made a mistake or forgot something.
Ok now what do I want number to equal when none of the switch's have been pushed because 0,1,2,4 or 8 was written to the memory address call "number" and it will stay there until I write over it with a new value.
maybe I could use what ever is in the memory address "number" to run a few lines of code then -1 until it equals 0 then stop running that code until the button is pressed again.
p.s when you find that this code is not working correctly and seems kinda random go to the learning section and look at INPUT_PULLUP then do a little reading.