Reading a Button and Serial Communication

Activities With Digital Read Serial 

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

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


Learning Goals:

After completing these activities, you should understand:

 the input command digitalRead,  

Setup Function Serial.begin 

The output commands Serial.print  and Serial.println



Open the Digital Read Serial program from the examples in Arduino Create

/*

 DigitalReadSerial

Reads a digital input on pin 2, prints the result to the Serial Monitor

This example code is in the public domain.

https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial

*/

int pushButton = 2;         // digital pin 2 has a pushbutton attached.  In this program call pin 2 pushButton:

int buttonState = 0; // Creates a space in the  Arduinoś memory to store the button's condition, called buttonState

void setup() {           // the setup routine runs once when you press reset:

Serial.begin(9600);  // Prepares the Arduino to send serial communications to the attached computer at 9600 bits per second:

pinMode(pushButton, INPUT); // Connects  pushbutton's pin to the input circuitry of the Arduino 

}


void loop() { // the loop routine runs over and over again forever:

buttonState = digitalRead(pushButton);  // read the input pin and saves the value in buttonState:

Serial.println(buttonState); // print the value stored in buttonState to the serial monitor:

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

}


Explanations and definitions:


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

The buttonState variable 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 Serial communications that 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:  You can see the Serial communication by clicking the screen icon in the bottom right of the Tinkercad Screen. 

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 comm-port. You can send variables or text in quotes.  After the print data is sent, a line command moves 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 

Add a second button circuit to your Tinkercad Diagram similar to the first but connected to a different  pin on the Arduino 

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 TinkerCad  Diagram of your circuit?

Repeat the above steps using the actual Arduino and circuit components and the Ardunio Create For Education app

You can see the Serial communication by clicking the monitor button to the left of the code window in Arduino Create


Take a 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 in your activity report.

Attach your completed Activity Report to the assignment in Google Classroom