Translate

Tuesday, 4 March 2014

Room Management System – Coding the input stage - part 1


Let's start make the input work. I guess you all have been through the Arduino “Hello World tutorials” and I don't have to go through how to declare variables, what has to go into the setup and what are functions.
I am going to put down the code with as many remarks and explanations as possible.

We start declaring some variables:

////////////////////////////////////Declaring the Variables//////////////////////////////////////////////////

////////////////////////////////////defining Arduino Pins///////////////////////////////////////////////////

int latchPin = 2; //latchPin connected to Arduino digital pin 2
int clockPin = 3; //clockPin connected to Arduino digital pin 3
int dataPin = 4; //dataPin connected to Arduino digital pin 4

/////////////////Variables to hold the data for each shift register///////////////////////////////

byte switchVar1 = 0; //Data for input shift register 1
byte switchVar2 = 0; //Data for input shift register 2
byte switchVar3 = 0; //Data for input shift register 3

/////////////////////////////////all the other variables///////////////////////////////////////////////////
unsigned int switchState[25] = {0}; array holding the state of each switch

void setup() {
//////////////////////////////////Start Serial for Debugging////////////////////////////////////////

Serial.beginn(9600);

////////////////////////////////////////defining pin modes////////////////////////////////////////////

pinMode(latchPin, OUTPUT); //setting the latch pin to output
pinMode(clockPin, OUTPUT); //setting the clock pin to output
pinMode(dataPin, INPUT); //setting the data pin to input
}


Up to this point is everything pretty straight forward. Under defining the Arduino pins, we just assign the digital input pin of our Arduino chip to a variable. To keep track of what we are doing, I am using the name of the shift register pin, the assigned Arduino pin is connected to. Like
int latchPin = 2;
Under “Variables to hold the data for each shift register” we are defining the variables where the readout of our shift registers will be stored. The data held will be in form of a binary number.
If no button was pressed the output will be 00000000 for each shift register. If the button connected to the shift register input 3 was pressed the output will be 00000100 or if the button connected to input 3 and input 8 were pressed, the output is 10000100. The same thing happens with the other 2 remaining shift registers. Putting everything together, depends on the buttons being pressed or in praxis depens which switch is activated, we might get a 24 bit binary like 101001101000010011000000.
Don't worry, the data processing is always split up in the single shift registers and we are always dealing with 8 bit binary junks.
The next variable is again straight forward.
unsigned int switchState[25] = {0};
Since we don't work with negative numbers, we add “unsigned” to our int declaration to reserve only the memory we need for our variables. Why declare 25 places in our array when we have only 24 input pins. I am not sure why but if the declaration is exact 24 and we work with the 24th space in the array it returns sometimes some unpredictable behaviour. I made it a habit to go always 1 over the needed places.
In the setup loop we start serial communication to do some readout in the serial window for debugging.
Next we are declaring the pin modes, telling our Arduino chip how to handle the previously defined pins.

In the following post we start coding the main loop.

No comments:

Post a Comment