For this assignment, I studied the behavior of time and created a device that is a simple timer/clock. Using the Arduino, I created a program that measures 30 seconds in human time, and upon reaching its goal, blinks the 2 LEDs with great joy at reaching its goal [and finishing this assignemnt] (and then quickly setting back to repeat this task over and over again).
// Import libraries. #include <Servo.h></pre> // ================================================================================ // Define constant values and global variables. // The wiring assignment. #define LED1PIN 5 #define LED2PIN 6 #define SERVO_PIN 9 #define BLINKDELAY 100 // in milliseconds int CurrentAngle; // Create an object to control the servo by declaring it. The Servo C++ class // is defined in the Servo library. Servo servo; // ================================================================================ // Configure the hardware once after booting up. This runs once after pressing // reset or powering up the board. void setup() { // Initialize the serial UART at 9600 bits per second. Serial.begin(9600); // Initialize the Servo object to use the given pin for output. servo.attach(SERVO_PIN); } // ================================================================================ // Linear servo movement function. This will step from the start angle to the // end angle as requested. This emits servo updates at a constant rate. It // does not return until the movement is complete. // // start - angle in degrees // end - angle in degrees // speed - optional argument, speed in degrees/sec // void linear_move(int start, int end, float speed = 6.0) { // Specify the number of milliseconds to wait between updates. const int interval = 20; // Compute the size of each step in degrees. Note the use of float to capture // fractional precision. The constant converts speed units from milliseconds // to seconds: deg/step = (deg/sec) * (sec/msec) * (msec/step) float step = speed * 0.001 * interval; // Declare a float variable to hold the current servo angle. float angle = start; // Begin a do-loop. This always executes the body at least once, and then // iterates if the while condition is met. do { servo.write(angle); // update the servo output delay(interval); // pause for the sampling interval if (end >= start) { angle += step; // movement in the positive direction if (angle > end) angle = end; } else { angle -= step; // movement in the negative direction if (angle < end) angle = end; } } while (angle != end); // Update the servo with the exact endpoint before returning. servo.write(end); } // ================================================================================ void loop() { for (int i = 0; i < 10; i++) { CurrentAngle = 180; linear_move(0, CurrentAngle); if (CurrentAngle = 180) { for (int i = 0; i < 3; i++) { digitalWrite(LED1PIN, HIGH); // turn LED1 on delay(BLINKDELAY); digitalWrite(LED1PIN, LOW); // turn LED1 off digitalWrite(LED2PIN, HIGH); // turn LED2 on delay(BLINKDELAY); digitalWrite(LED2PIN, LOW); // turn LED2 off delay(BLINKDELAY); } } linear_move(180, 0, 600); CurrentAngle = 0; } }
Leave a Reply
You must be logged in to post a comment.