User:Sej8926/ENES-100/project 3

=Week 1 Narrative= Based on the flow cart from last week, which is again placed below, I can add to the code to make it go through all 64000 combinations.

Taking each box in the flowchart, I created the code.

/********************************************************************************************** * This program uses the Adafruit Motor shield library v2 to spin a motor that spins a lock * for every possible combination. * * Adafruit Motor Shield Library manual can be downloaded at: *          http://learn.adafruit.com/downloads/pdf/adafruit-motor-shield.pdf * *  *********************************************************************************************/


 * 1) include 
 * 2) include 
 * 3) include 


 * 1) define DIGITS         40
 * 2) define STEPS_PER_REV  200
 * 3) define STEPS_PER_DIGIT 5

Adafruit_MotorShield AFMS = Adafruit_MotorShield;

// steps/revoution Adafruit_StepperMotor *motor = AFMS.getStepper(STEPS_PER_REV,2); int solenoid = 6;                                    //set solenoid to digital pin 6 int first; int second; int third; // This function sets the parameters for the serial link, solenoid and motor void setup { Serial.begin(9600);    // set up Serial library at 9600 bps Serial.println("Complete test of stepper motor."); AFMS.begin;           // create with default frequency 1.6 kHz motor->setSpeed(120);   // 120 rpm // initialize the digital pin as an output pinMode(solenoid, OUTPUT); digitalWrite(solenoid, LOW); } void loop { //continue interations until lock is opened combinations; while(1) { }             // All combinations have been tried wait for a new program } void combinations {  // Start with three full forward rotations motor->step(600,FORWARD, DOUBLE);

for (first = 1; first < DIGITS; first++) {   for (second = 1; second < DIGITS; second++) {     for (third = 1; third < DIGITS; third++) {       motor->step(first*STEPS_PER_DIGIT, FORWARD, DOUBLE); delay(250);                    // wait for 1/4 second motor->step(STEPS_PER_REV, BACKWARD, DOUBLE); motor->step(second*STEPS_PER_DIGIT, BACKWARD, DOUBLE); delay(250);                    // wait for 1/4 second motor->step(third*STEPS_PER_DIGIT, FORWARD, DOUBLE); delay(250);                    // wait for 1/4 second digitalWrite(solenoid, HIGH);  // turn the solenoid on (HIGH is the voltage level) delay(20);                     // wait for a 0.02 seconds digitalWrite(solenoid, LOW);   // turn the solenoid off by making the voltage LOW delay(100);                    // wait for a tenth of a second

Serial.print("Combination attempted is "); Serial.print(first); Serial.print(", "); Serial.print(second); Serial.print(", "); Serial.print(third); Serial.print(".");

//TODO: Check light sensor to see if lock opened. If so return out of loops // return;

//finish with three full forward rotations motor->step(3*STEPS_PER_REV,FORWARD, DOUBLE); delay(1000); }    }  } }

However, there is one slight problem in the code that needs to be fixed, and it is difficult to explain. Take for example the code tested out the combination "5-10-35" and it was unsuccessful at opening the lock. It next step to do is to rotate 3 full rotations to clear the combination, and then it will move over one digit to start the new combination. With these, we will want the next combination first digit to be a 6 and not a 5. However, if it does 3 full rotations, it will end on 35. Then it will move over one digit, and start at 36 which we do not want.

Next Steps:
 * Fix the above problem
 * Add in the code that involves the Vernier PhotoGate
 * Adjust the code to make it go through the algorithm to only have 100 possible combinations instead of 64000


 * I love it! A problem. It is the difference of being relative or absolute ... the absolute solution would be to not spin three revolutions, but to spin at least three times and then to zero at the end of each failed attempt, the relative solution would be to remember the last position of the lock and create a new first command relative to it (or zero if starting from scratch) ... so the next step would be to outline both of the above, I like returning to zero each time because this would help us "see" any hysteresis ... 10/10/60--1sfoerster (discuss • contribs) 16:58, 21 April 2014 (UTC)

=Week 2 Narrative=
 * 0/0/0 .. nothing to grade --1sfoerster (discuss • contribs) 15:51, 22 April 2014 (UTC)

=Week 3 Narrative= There are two problems that need to be fixed:
 * 1) The solenoid is not firing at the correct time
 * 2) The bug described in Week 1

To fix the first problem, there are two possible solutions:
 * 1) Increase the RPM
 * 2) Increase the delay in between digits

The best solution is to increase the DELAY so there is more time for the motor to process. ALSO, it allows me to better see what the code is doing, which allows me to verify that it is doing what it is supposed to do. What I did was I changed the delay from 250 ms to 1250 ms(1.25 seconds). Only in the section of the code where it tells the motor to spin to the first, second, and third digit.

To fix the next problem (the bug) after the three full rotations, the motor has to spin the combo lock back to zero. To do this, it spins three full rotations and then 40-the third digit, because there are 40 digit on the combo lock. If the third digit is 20, the motor spins the combo three times and it ends back on 20; NOW it is going to spin 40-20, which is 20. Once it spins the 20 more digits, it will end up on 0. To ensure that the motor is doing this, I increased the delay from 1000 ms to 10000 ms (10 seconds) I changed it to such a large delay, because after the motor spins 3 full rotations, that would have taken 9 seconds (it is going 120 rpm to go through 40 digits, so that makes it 3 seconds for a full rotation).

//uses Adafruit Motor shield library
 * 1) include 
 * 2) include 


 * 1) include 

Adafruit_MotorShield AFMS = Adafruit_MotorShield;

//AF_Stepper motor(200, 2); Adafruit_StepperMotor *motor = AFMS.getStepper(200,2); int solenoid = 6; //set solenoid to digital pin 6 int first; int second; int third; void setup { Serial.begin(9600);   // set up Serial library at 9600 bps Serial.println("Complete test of stepper motor."); AFMS.begin;           // create with default frequency 1.6 kHz motor->setSpeed(120);   // 120 rpm // initialize the digital pin as an output pinMode(solenoid, OUTPUT); digitalWrite(solenoid, LOW); } void loop { //continue interations until lock is opened combinations; } void combinations { for (first = 1; first < 40; first++){ for (second = 1; second < 40; second++){ for (third = 1; third < 40; third++){ motor->step(first*5, FORWARD, DOUBLE); delay(1250); motor->step(200, BACKWARD, DOUBLE); motor->step(second*5, BACKWARD, DOUBLE); delay(1250); motor->step(third*5, FORWARD, DOUBLE); delay(1250); digitalWrite(solenoid, HIGH);  // turn the solenoid on (HIGH is the voltage level) delay(20);                    // wait for a 0.02 seconds digitalWrite(solenoid, LOW);   // turn the solenoid off by making the voltage LOW delay(100);                    // wait for a tenth of a second Serial.print("Combination attempted is "); Serial.print(first); Serial.print(", "); Serial.print(second); Serial.print(", "); Serial.print(third); Serial.print("."); //finish with three full forward rotations plus correction int x=600+(40-third); motor->step(x,FORWARD, DOUBLE); delay(10000); }    }  } }

I verified the code to make sure there were no errors.
 * 10/10/30 ... need video for more points ... lots more video of the tiniest success ... need to celebrate this --1sfoerster (discuss • contribs) 15:46, 29 April 2014 (UTC)

=Week 4 Narrative= I did an error analysis because I suspected that the step rotations were not consistent with each trial. SO I decided to quantify these inaccuracies by performing two experiments. This required me to change the code to only go one single step, and then change it again to go one full rotation

First Experiment: Single Motor Step for 10 Trials Mean: 2.4 (The error is over 100%)
 * 2
 * 2
 * 3
 * 1
 * 3
 * 3
 * 3
 * 3
 * 3
 * 1

Second Trial: One Full Rotation Mean: -8.2 (20% Error)
 * -7
 * -7
 * 1) -11
 * -7
 * 1) -10
 * -7
 * 1) -13
 * -7
 * -7
 * -6

It can be seen that the motor is not accurate enough to try out every combination consistently. Because of this, it cannot be determined which combinations have been tried and which ones haven't.


 * 10/10/20 Excellent observation, the test needs to be more formally documented. Please document the arduio code, please describe what the expected numbers are, please describe how the numbers were observed ... --1sfoerster (discuss • contribs) 13:32, 6 May 2014 (UTC)