Document each exercise and the process for completing it (what went well? what was difficult?), including taking a video or screen capture. In addition, create a schematic for each circuit that you made and reflect on the interaction involved for each step. Upload these along with your homework to the documentation blog.
PING PONG
The most important thing I have learned while undergoing these processes is the importance of taking things step by step. While the overall idea was to make it appear as if the motors are hitting the ball back and forth, we had to break this down to make sure we knew how each part worked individually.
Step 1: MOTORS✅
first we made sure that we knew how to connect the motors and program them in arduino.
Schematic
Step 2 CODING IN PROCESSING✅
Next we made the ball appear as if it is bouncing back and forth. We had some difficulty on finding the right code for this. However, after trial and error we figured it out.
Step 3: COMBINATION✅
This step didn’t require much critical thinking. All we had to do was ensure that processing was sending the proper values to the arduino and the arduino was responding properly to the values. In this case, processing sends one value: x position. The arduino makes one motor turn if x=0 and the other turn if x=1500(width).
Step 4: AESTHETICS✅
Finally, my partner and I each created a hand to make it look like two hands hitting the ball back and forth.
Processing code
import processing.serial.*; int NUM_OF_VALUES_FROM_PROCESSING = 1; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int processing_values[] = new int[NUM_OF_VALUES_FROM_PROCESSING]; //** this array stores values you Serial myPort; String myString; int x = 0; int speed = 30; void setup() { background(0); size(1500, 1600); setupSerial(); } void draw() { sendSerialData(); background(0); circle(x, 300, 90); x = x+speed; if (x>width -1 || x<0) { speed = -speed; } processing_values[0] = x; } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[3], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII myString = null; } void sendSerialData() { String data = ""; for (int i=0; i<processing_values.length; i++) { data += processing_values[i]; //if i is less than the index number of the last element in the values array if (i < processing_values.length-1) { data += ","; // add splitter character "," between each values element } //if it is the last element in the values array else { data += "\n"; // add the end of data character linefeed "\n" } } //write to Arduino myPort.write(data); print(data); // this prints to the console the values going to arduino }
Arduino Code
#define NUM_OF_VALUES_FROM_PROCESSING int tempValue = 0; int valueIndex = 0; int processing_values[NUM_OF_VALUES_FROM_PROCESSING]; #include <Servo.h> Servo myservo; Servo myservo2; void getSerialData() { while (Serial.available()) { char c = Serial.read(); switch (c) { //if the char c from Processing is a number between 0 and 9 case '0'...'9': tempValue = tempValue * 10 + c - '0'; break; case ',': processing_values[valueIndex] = tempValue; //reset tempValue value tempValue = 0; //increment valuesIndex by 1 valueIndex++; break; //if the char c from Processing is character 'n' //which signals that it is the end of data case '\n': //save the tempValue //this will b the last element in the values array processing_values[valueIndex] = tempValue; //reset tempValue and valueIndex values //to clear out the values array for the next round of readings from Processing tempValue = 0; valueIndex = 0; break; } } } int pos = 0; // variable to store the servo position void setup() { Serial.begin(9600); myservo.attach(10); myservo2.attach(9);// attaches the servo on pin 9 to the servo object } void loop() { getSerialData(); if (processing_values[0] == 1500) { for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(2); // waits 15 ms for the servo to reach the position } for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(5); } } if (processing_values[0] == 0) { for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo2.write(pos); // tell servo to go to position in variable 'pos' delay(2); // waits 15 ms for the servo to reach the position } for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo2.write(pos); // tell servo to go to position in variable 'pos' delay(5); } } }