Recitation 8: Serial Communication

In this recitation, you will complete the following exercises to send data from Arduino to Processing, and vice versa, using serial communication. You will be using the SerialRecord library.

Task #1:  Make a Processing Etch-A-Sketch

etch-a-sketch
Image from ebay.com

To see how an Etch-A-Sketch works, you can watch a video here.

Arduino example

Working individually, for this exercise, use Arduino to send two analog values to Processing via serial communication. To do this, build a circuit with two potentiometers and write an Arduino sketch that reads their values and sends them serially. (Use SerialRecord example SendMultipleValues as a basis):

 

processing example

 

 

 

 

Then, write a Processing sketch that draws a circle and reads those two analog values from Arduino. (Use SerialRecord example ReceiveMultipleValues as a basis). This sketch should modify the circle’s x and y values based on the input from Arduino. Just like an Etch-A-Sketch, one potentiometer should control the circle’s x-axis movement, and the other should control the circle’s y-axis movement. 

Once you have this working with a circle, modify the code to use a line instead (like a real Etch-A-Sketch).  To do this, you will need to keep track of the previous x and y values so that you can draw a line from there to the new x and y positions. You can use this code as a reference.

Task #2:

Working as pair, write a Processing sketch in which a ball bounces from left to right on the screen. (You can use the fullScreen() function, instead of size(), to make the Processing window occupy the full dimensions of your screen.) Send values from Processing to Arduino based on the position of the bouncing ball. You can start with the example code in SerialRecord, SendMultipleValues.  These values should somehow give an indication of when the ball hits the edge of the screen. Then, make a circuit with your Arduino and a servo motor. The corresponding Arduino code should read the serial values from Processing and translate them into movements of the servo motor as if it is hitting the ball.  You can use the SerialRecord code as a starter, RecieveMultipleValues. See the animated image below for reference as an example built with two servo motors, you can build it with one instead if you are working individually. 

Documentation

For your blog post, this week include documentation of task #1, task #2, and the Homework.

Task #1 Documentation

  • Document your process for completing the task: (what went well? what was difficult? any surprises?).
  • Include a reflection on the interaction involved.
  • Include a video and a screenshot.
  • Embed your code.

Task #2 Documentation

  • List the name of your pair teammate.
  • Document the process for completing the task: (what went well? what was difficult? any surprises? what was your process of teamwork?).
  • Include a reflection on the interaction involved.
  • Include a video and a screenshot.
  • Embed your code. 

Recitation 3: Workout

For this week’s recitation, you will be also doing some workout! You will use the tilt switch from your kit to build a wearable workout sensor, do a workout exercise, and complete a challenge. You will work in pairs, but each person will need to make your own circuit.

Remeber to document the process for your blog documentation, and futher descriptions about the documentation can be found at the end of the post.

Materials:

From your kit:

        • 1 * Arduino Uno
        • 1 * USB A to B cable
        • 1 * breadboard
        • 1 * tilt switch
        • 1 * 10k ohm resistor
        • A handful of M/F, M/M, and F/F cables

From course supplies:

        • 2 long wires (about 1.8 m)
        • 1 capacitor 0.1 uF (confirm size)

Recitation Exercise:

Step 1: Prepare your tilt sensor

Task #1: Solder the sensor to two long wires. Pay attention to your safety and take care of your workplace. You can optionally cover the soldered parts with electrical tape or a heat-shrink tube for safety and stability.

Task #2: Connect the tilt sensor cables to your Arduino using a capacitor as the circuit below:

tilt-22uF-10k

Task #3: Program your Arduino with the following sketch. Confirm that you get an input from your tilt sensor on the the Serial Monitor to check whether it is working correctly.

int SENSOR_PIN = 2;    
int tiltVal;

void setup() {
  pinMode(SENSOR_PIN, INPUT);    // Set sensor pin as an INP
  Serial.begin(9600);
}

void loop() {
  // read the state of the sensor
  tiltVal = digitalRead(SENSOR_PIN);
  Serial.println(tiltVal);
  delay(10);
}

Step 2: Refine the code

Task #1: Upload the following sketch to only print 1 when the tilt sensor changes from 0 to 1, and 0 when from 1 to 0. For example, the code we used before might  have printed 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 as you move the sensor between upright and upside-down, but now your code should print 0 1 0 1.

int SENSOR_PIN = 2;
int tiltVal;
int prevTiltVal;

void setup() {
  pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin
  Serial.begin(9600);
}

void loop() {
  // read the state of the sensor
  tiltVal = digitalRead(SENSOR_PIN);
  // if the tilt sensor value changed, print the new value
  if (tiltVal != prevTiltVal) {
    Serial.println(tiltVal);
    prevTiltVal = tiltVal; 
  }
  delay(10);
}

Step 3: Wear your sensor

Fix the tilt switch using paper tape to your body, making it a wearable sensor. The sensor should be along your forearm, so that you can use it to detect when your forearm is facing up.

Step 4: Bicep Curl Workout!

Image from Icedgif.com

 

Task #1: Add a conditional to your sketch so that it shows  a message on the Serial Monitor ONLY when a full biceps curl has been completed. Remember to switch from the Serial Plotter to the Serial Monitor. You can comment existing lines of code with //.

You will need to :

          1. Identify the proper section in your code where you can add code when transitions happen.
          2. Add a conditional that has the criteria so that you only detect transitions LOW to HIGH, showing only when the user has raised their arm completely. 

Task #2: Count the curls, adding one to its value every time that the user does a complete bicep curl. You should:

          1. Declare a global variable.
          2. Add one to its total every time that a LOW to HIGH transition has happened.
          3. Modify the Serial.print and Serial.println instructions so that the value of this new variable is shown on the Serial Monitor.
    1.  

Task #3: Set a limit to the variable you are using to count the curls, so that the final code has the following behavior.:

          1. When the total reaches 8, Print “Yay, you’ve done one set of curls”. Hint: you will need another conditional
          2. Reset the count, so that the behavior repeats at 16 curls, 24, etc.

Step 5: Exercise Challenge

You should design a different physical exercise (not bicep curls). The user will have to wear the sensor according to the physical movement, you should test it with a partner, and receive feedback.

Some ideas that could complement this challenge:  

        • Add a buzzer and modify the code so that it “celebrates” each completion of each set of exercise.
        • Add a vibration motor to your circuit and modify the code so that it  gives haptic feedback for each exercise.
        • Add a potentiometer to your circuit, so that the user can configure the number of repetitions in a set.
        • Add a second tilt sensor to your circuit. Count two people’s exercises on the same Arduino.
        • Give the user instructions to follow using the Serial.print. For example, when the sketch starts, it prints “Start your Workout”.
        •  Make it a time sensitive activity. For example, after 20 seconds, it stops counting and it prints “STOP! Your time is up!”.
        • Detect when the user has stopped doing the exercise before the set is done. Prompt them on the Serial Monitor. 

Documentation:

      1. Record a short video of yourself doing step 4 (bicep curls) and 5 (challenge) while showing the Serial monitor/plotter in action.  

2. Embed your code into your blog as shown with the example codes in this recitation, NOT by taking a screenshot of it.

3. Draw your own illustration sketches that showcase how a person would use this interactive training device.

4. Reflect on your experiments, explaining the notes you took, and adding photos, and/or screen shots to document what tests you have tried and what you have learned from them. As a guidance, use the following questions:

          • At what angle of tilt does it transition between HIGH and LOW?
          • What else did you notice about its behavior?
          • What if you rotate the limb that has the sensor attached to it?
          • What if you shake it?
          • What if you hold the wires several centimeters away and tilt it?
          • Do you think it can be used by any user?