Reading a Button and Serial Communication

Activities With Digital Read Serial

Learning Goals:

After completing these activities you should understand the input command digitalRead, Setup Function Serial.begin and the output commands Serial.print and Serial.println

The Digital Read Serial Activity introduces several important ideas, these activities will help you understand those concepts so that you may use them in your own programming.

Any button connected to the Arduino has 2 pieces of information or variables associated with it, in the program below I have highlighted the two variables.



/*

DigitalReadSerial

Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.

*/

// digital pin 2 has a pushbutton attached to it. Give it a name:

int pushButton = 2;

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// make the pushbutton's pin an input:

pinMode(pushButton, INPUT);

}

// the loop routine runs over and over again forever:

void loop() {

// read the input pin:

int buttonState = digitalRead(pushButton);

// print out the state of the button:

Serial.println(buttonState);

delay(1); // delay in between reads for stability

}

pushButton is the location or pin that the button is connected to, and

buttonState is the data being input at that location.

The Button state does not change automatically when the button is pushed but only when we read the condition with the digitalRead command

Serial Commands

This activity also introduces the use of Serial communications to send information from the Arduino to the computer through the USB and com port. There are three commands you need to be familiar with to do this:

Serial.begin(9600); This command opens the communication port so we can send information between the computer and the Arduino.

Serial.println( "information to print" ); This command will send the content of the brackets to the com port you can send variables or text in quotes. After the print data is sent a line command is sent to move the curser to the next line.

Serial.print(" information to print"); This works the same as Serial.println but the line command is not sent so the next item printed will appear on the same line.

Add Serial Print lines to your sketch explaining the output

Move the button to a different input pin

Change the pin the button is connected to from pin 2 to some other pin between 3 and 13, Change the pin number in the int pushbutton = statement to the new pin number.

Upload your sketch and see if it works connected to the new pin.

Add a Second Button

To add a second button you will need to:

Add a second location variable

Make that pin an input with the pin mode command

Add a second buttonState variable for the new button

Add a second serial.println statement to output the new data

Complete a Fritzing Diagram of you circuit?

Take and photo of your Completed circuit.

Take a Screen capture of your serial monitor output showing all possible conditions.

(alt print screen will place a screen capture of the active window on your clipboard that you can paste into your document )

Answer the following Questions.

What is the differance between print and println commands?

To use a button what two variable must you create/

Explain command line int buttonState = digitalRead(pushButton);

/*

DigitalReadSerial

Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.

*/

// digital pin 2 has a pushbutton attached to it. Give it a name:

int pushButton = 2;

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// make the pushbutton's pin an input:

pinMode(pushButton, INPUT);

}

// the loop routine runs over and over again forever:

void loop() {

// read the input pin and save the value in the variable buttonState.

int buttonState = digitalRead(pushButton);

// Send the buttonState information to the computer through the USB serial connection:

Serial.println(buttonState);

delay(1); // delay in between reads for stability

}