Week 29. Mar 18.22 2019 - michelle-qin/Portfolio GitHub Wiki
This week, I am working on eliminating the pause/delay in the Ball Rotation's cycle. We want it to flow in one continuous cycle.
Yesterday, Mr. Harlow and I hooked up a logic analyzer to the board and we noticed that if we create a for loop that runs between the values of 330 and 360, the rotation is static (it doesn't move). This might be helpful... so I will try to implement this in my code.
My current code:
//BALLROTATION by Michelle//
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
bool sensor_passed = false;
int num_of_cycles = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello");
pwm.begin();
pwm.setPWMFreq(50);
}
void loop() {
// put your main code here, to run repeatedly:
int sensor_outside = digitalRead(5);
int sensor_inside = digitalRead(6);
if (sensor_inside == 1) { //no metal
pwm.setPWM(0, 1024, 3072);
Serial.println(sensor_inside);
}
else { // sensor_inside = 0
Serial.println(sensor_inside);
if (sensor_outside == 0) { // 0 = metal
sensor_passed = true;
while (sensor_passed == true) {
pwm.setPWM(0, 1024, 3072);
Serial.println(sensor_inside);
delay(400);
if (sensor_inside == 0) {
sensor_passed = false;
}
}
pwm.setPWM(0, 0, 4096);
delay(500);
}
else {
pwm.setPWM(0, 0, 4096);
delay(500);
}
}
}
void updateSensorReading() { int sensor_inside = digitalRead(6); while (sensor_passed = true) { Serial.println(sensor_inside); pwm.setPWM(0, 1024, 3072); Serial.println("Hello"); if (sensor_inside == 0) { sensor_passed = true; } else { sensor_passed = false; } } pwm.setPWM(0, 0, 4096); sensor_passed = false; delay(5000); }
I worked with a mentor this past Wednesday and found success! Turns out that the delay(5000) was the problem. Once I commented that out, it worked like a charm. I documented the code, renamed some variables to make it more clear, and pushed the code to GitHub.
This week, I got started on another interactive part for the Ball Wall: the Archimedes staircase. With this part, it is a long cylinder with a spiral staircase inside it and we want the ball to be able to enter the cylinder at the base and be transported to the top by the staircase rotation (by servo motor). After trial and error (and great advice from Mr. Miller), this was also a success. After Spring Break, I plan to document the code and push it to GitHub. See below for code.
//SERVO MOTOR RANGE TESTER//
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
bool sensor_passed = false;
#define METAL_DETECTED 0
#define SERVOMIN 150
#define SERVOMAX 600
#define SERVOSUPERMAX 4000
#define SERVOMED 375
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60);
// copied from servo demo code
delay(10);
}
void loop() {
// put your main code here, to run repeatedly:
delay(500);
int sensor_ball = digitalRead(5);
if (sensor_ball != METAL_DETECTED) { //no metal detected
pwm.setPWM(0, 0, 0); // do not run
Serial.println("Not detected :(");
} else { // here, sensor_motor is detecting metal
Serial.println("Detected :)");
for (uint16_t pulselen = 280; pulselen < 340; pulselen+=1) {
pwm.setPWM(0, 0, pulselen);
Serial.println("Pulse length:");
Serial.println(pulselen);
delay(500);
`// note: at abuot pulselen ~ 340 the servo seems to switch direction,`
`// with the servo rotating a different speeds around the switch point.`
`/*`
`sensor_ball = digitalRead(5);`
`if (sensor_ball == METAL_DETECTED) {`
`Serial.println("Double detect. Stopping! :|");`
`break;`
`}`
`*/`
`}`
}
}