Adding a switch to Arduino data collection

Once an Arduino measurement system is reporting measurements such as distance described in the previous post, it's nice to add the capability to turn the data reporting on and off. This is because the Arduino continuously loops, making it difficult to extract "real" data from the stream of values being reported. One could add a delay to slow the reporting, but this will affect measurements like motion by limiting the range precision of position measurements and thus reducing the range of velocities that are measureable.

Let's look at having the Arduino report measurements only when a switch is "on". This is going to require us to understand the idea of digital input and output (I/O), which is different than the analog input we measure with the ultrasonic sensor.

A switch is added to our distance sensor setup. It is connected to three digital pins 4, 5, and 6.
A circuit can be constructed as shown above, where three digital pins (4, 5,6) from the Arduino are connected to the poles of a switch. The two outer digital pins (4 and 6) will be set to output particular values. Pin 4 will be digital HIGH or 1, and pin 6 will be digital LOW or 0. The center pole is connected to pin 5, which will be an input value we read. When the switch is left, it connects the middle input pin 5 to pin 6. In this case we read LOW or 0. We will equate this to "off", and will not report measurements to the Serial port. When the switch is right, it connects the middle input pin 5 to pin 4. In this case we read HIGH or 1. We will equate this to "off", and will report measurements to the Serial port. The conditional reporting of data is accomplished using an if-then-else statement as shown. We will first look at code for the switch only. Then, we will insert our distance sensor code appropriately.
 //This program will set digital pins 4 and 6 to HIGH and LOW.
 //It will also read the digital pin 5.
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //open communication b/w the Arduino and computer
  pinMode(4, OUTPUT);//set digital pin 4 as output
  pinMode(5, INPUT);//set digital pin 5 as input
  pinMode(6, OUTPUT);//set digital pin 6 as output

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(4, HIGH);
  digitalWrite(6, LOW);
  if(digitalRead(5) == HIGH){
    //check to see if the switch is "on"
    Serial.println("report data");//if "on" send to the serial port 
    //if the switch is "off" 
    //do nothing and move on 
Compile and upload this code to your Arduino. Using the Serial Monitor, check that the switch functions properly. Once it is functioning properly, see if you can modify the program to include the distance sensor reading from our last lab. Don't forget there is a variable declaration at the beginning of the distance sensor program. The reading and reporting of the sensor should only occur if the switch is on.

Recompile and upload your code to check the functioning of your code. If it appears to work, test it in various conditions. For example, try to measure an object such as a ball rolling across the table. Use tape on the table to turn the switch on and off to capture the motion only while the ball is moving between the initial and final tape points.

Let's look at constant velocity motion with our sensors.

  1. Look at the following graphs. You will be given printed copies of them. Label on the graphs each section corresponding to motion that is different from the section before and after. In the label, include the \(\Delta x, \Delta t\), and average velocity, \(\langle v\rangle\). Also, write one sentence explaining what happens in that section of motion, e.g., “The object undergoes a displacement in the positive direction of 2 meters over 1 second, giving a velocity of 2 m/s in the positive direction.”

    Graph 1 of position vs. time.

    Graph 2 of position vs. time.

    Graph 3 of position vs. time.

  2. Use your motion sensor and Arduino to create duplicate graphs like these by moving in front of the sensor with a small whiteboard. You should be careful to produce the graphs as accurately as possible including the particular distances and times. Putting tape on the floor may help. Practice with the Serial Plotter, then use the Serial Monitor to collect data that you copy and paste into Excel. For the time variable, assume each data point corresponds to the delay of 100 ms. This is not quite right since the Arduino takes time to loop, but it is off by less than 1 ms.
  3. In Excel make graphs of your motion to include in a lab report.
  4. Expectations: The velocities are to be calculated correctly. Experimentally, the average velocity within each time segment of your LoggerPro graph should be within 1 m/s of the calculated value. Be sure to calculate these for each segment. Also the starting/ending times of each time segment of your LoggerPro graph should be within 1 second of those given in the position vs time graphs.
  5. Lab Report

    Write a summary report in Microsoft Word. You will save it to the shared OneDrive that I sent a link. Makes sure the relevant Excel work is in the Word document. I do not want your Excel document. In your lab report include:

    1. The three questions from last week.
    2. Do the answers to these questions affect the precision of today's measurements?
    3. The labeled handout graphs. Take a picture of them with a webcam or phone.
    4. Your Excel graphs and calculations of average velocity for each segment. Be clear about how you calculate average velocities by including \(x_i, x_f, t_i, t_f\).
    5. Describe the motion sensor's coordinate system. How does it define positive and negative?

No comments:

Post a Comment