WEEK 9 : Output Devices

Starting this week I want to do a quick recap from last week. last week I started testing some proximity sensors that would eventually go into the "moving coffee table" and connect it to a couple stepper motors. the sensors I tried were the ultrasonic sensor HC-SR04, the VL53L1X time-of flight sensor and the PIR Motion Sensor

.

I will add little text here because the videos below are eplained on last weeks post... after reading the inputs with the proximity sensors I used a couple motors to test what would be the best way to get the coffee table to move. while this was a fun first test for the initial prototype I realized quick I had to scale up.


                    #include Wire.h        // Include the I2C library
                        #include VL53L1X.h     // Include the VL53L1X library
                        #include Servo.h       // Include the Servo library
                        
                        VL53L1X sensor;
                        Servo myservo;  // create servo object to control a servo
                        
                        void setup() {
                          Serial.begin(115200); // Start the serial communication with a baud rate of 115200
                          myservo.attach(26);   // attaches the servo on GIO26 to the servo object
                          
                          Wire.begin();         // Initialize the I2C
                          Wire.setClock(400000); // Use 400 kHz I2C
                          
                          sensor.setTimeout(500); // Set a timeout for sensor reading
                          if (!sensor.init()) {
                            Serial.println("Failed to detect and initialize sensor!");
                            while (1); // Infinite loop stops the program if sensor init failed
                          }
                          
                          sensor.setDistanceMode(VL53L1X::Long); // Use long distance mode
                          sensor.setMeasurementTimingBudget(50000); // Allow up to 50000 us (50 ms) for a measurement
                          
                          sensor.startContinuous(100); // Start continuous readings at a rate of one measurement every 50 ms.
                        }
                        
                        void loop() {
                          uint16_t distance = sensor.read(); // Read the distance measurement from the sensor
                          if (sensor.timeoutOccurred()) { 
                            Serial.print(" TIMEOUT"); // Check if a timeout occurred
                          } else {
                            Serial.print("Distance: ");
                            Serial.print(distance);
                            Serial.println(" mm");
                          }
                        
                          if (distance  100) {
                            // If the distance is less than 50, start moving the servo
                            for (int pos = 0; pos = 180; pos += 1) { // goes from 0 degrees to 180 degrees
                              myservo.write(pos); // tell servo to go to position in variable 'pos'
                              delay(1); // waits 15ms for the servo to reach the position
                            }
                            for (int pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
                              myservo.write(pos); // tell servo to go to position in variable 'pos'
                              delay(1); // waits 15ms for the servo to reach the position
                            }
                          }
                        }
                        

            

Okay! so onto this week, I went to office hours and met with Jake this week I kind of vaguely described my project to him and he suggested maybe I start by testing using a stepper motor and to use the driver below found here by Polulu. Sorry for the terrible image

.

So I went ahead and tested the driver in order to see if I could get the stepper motor to work with the board I produced on electronic production week and a 9v battery. I also decided to use a breadboard to test the driver before trying to produce a board for it. It was fairly smooth I just added a capacitor to control any electricity spikes and we were up and running!

.

I also connected the VL53L1X time-of flight sensor onto the board since this was the sensor that I liked the most last week because it provided the most accurate readings. I also programmed it so the motor would step when the sensor reads a 2 inch proximity. I also want to mention the codes you will find below are a mix between chat gpt and I.

.

Here is the full code that connects the stepper motor to the time of flight sensor and makes it rotate back and forth


                          #include        // Include the I2C library
                            #include     // Include the VL53L1X library
                            
                            VL53L1X sensor;
                            
                            #define dirPin 26  // Direction pin connected to DIR on the A4988
                            #define stepPin 27 // Step pin connected to STEP on the A4988
                            #define stepsPerRevolution 200 // The number of steps per revolution for your motor
                            
                            // Variable to keep track of the current direction
                            bool direction = HIGH;
                            
                            void setup() {
                              Serial.begin(115200); // Start the serial communication with a baud rate of 115200
                              Wire.begin();         // Initialize the I2C
                              Wire.setClock(400000); // Use 400 kHz I2C
                              
                              pinMode(dirPin, OUTPUT);
                              pinMode(stepPin, OUTPUT);
                            
                              // Initialize motor direction
                              digitalWrite(dirPin, direction); 
                            
                              sensor.setTimeout(500); // Set a timeout for sensor reading
                              if (!sensor.init()) {
                                Serial.println("Failed to detect and initialize sensor!");
                                while (1);
                              }
                              
                              sensor.setDistanceMode(VL53L1X::Long);
                              sensor.setMeasurementTimingBudget(50000);
                              sensor.startContinuous(50);
                            }
                            
                            void loop() {
                              int distance = sensor.read(); // Read the distance from the sensor
                              if (sensor.timeoutOccurred()) {
                                Serial.println("Sensor timeout!");
                              } else {
                                Serial.print("Distance: ");
                                Serial.print(distance);
                                Serial.println(" mm");
                              }
                            
                              if (distance < 40) {
                                // Change the motor direction
                                direction = !direction;
                                digitalWrite(dirPin, direction);
                            
                                // Make the stepper motor take one full revolution
                                for (int step = 0; step < stepsPerRevolution; step++) {
                                  digitalWrite(stepPin, HIGH);
                                  delayMicroseconds(1000); // This delay controls the speed, lower it to increase speed
                                  digitalWrite(stepPin, LOW);
                                  delayMicroseconds(1000); // This delay controls the speed, lower it to increase speed
                                }
                              }
                            
                              delay(100); // Small delay to avoid constant polling, adjust as needed
                            }
                                

                    

I then decided to test this onto a scaled prototype of the coffe table to see if I could get the same motion I did with the small prototype before.

.

I also designed some little connections in order to attach the motor onto the piece itself

.

I tested the motor one more time before attaching it to the table and it works fairly well! in my mind this motion would act as some sort of puppet driver and make the table walk

Aaaandd it doesnt work :'( the table cannot really walk and carry its own weight. its more like a sad shaking table feeling the Boston winter.

Anyways... Even though my heart was a bit broken that the table didnt work I decided to go back to the drawing board. I met again with Jake in office hours and he suggested I look into flexure design and start embedding servo motors onto the design itself. More to come..

.