Mouse Trap: Code

For our final project, we were able to utilize and expand on a good amount of the code we had from Labs 3 and 4. There were two main components to our design which we had to figure out how to integrate with the A3BU- a set of simple photo resistors and a servo motor from a hobby helicopter.

Had we been using an Arduino, we could have used a specific library for servo motors and with a few simple commands get the motor to do what we needed. With the A3BU, figuring out the commands was a little more involved. We started with our PWM code from the fan in lab 4, but none of the values we had used in that lab produced a result. Recognizing that the servo was designed to only actuate for specific signals, we wrote a simple for loop to increment our PWM values and watched to see what values produced a reaction in the servo. We identified a single value which would cause the motor to turn 45°, which was what we needed to release the bar of our trap, and a series of values we could send to reset it to a locked position on command.


if (n1 > (o1*2) || n2 > (o2*2)) //executes if time to charge capacitor doubles from one reading to next.

{

pwm_set_duty_cycle_percent(&myservo[0],425); //turn servo to release bar, if trap is set

 

if (read_char == 114) //r for reset

 

{

for (i = 171; i < 180; i++) //sends values to turn servo back to starting position

{

dtostrf(i,3,2,temp_str); //convert float to string

&nbsp;

gfx_mono_draw_string(temp_str, 0, 0, &sysfont);

&nbsp;

pwm_set_duty_cycle_percent(&myservo[0],i);

&nbsp;

}

}

}

The code to release the bar is executed when a significant change in the light hitting the photo resistors is recorded. The code to reset the bar is called when, after being tripped, the reset command is entered in the terminal.

Our next coding challenge was to get readings from the photo resistors. We wired a simple circuit with just our photo resistor, an LED and an additional resistor. Doing this, we verified the photo resistor was working and that by changing the light hitting the photo resistor, we affected the output voltage of the circuit, causing the LED to dim accordingly. Once we knew a changing voltage was being output, we wired the output to one of the A3BU’s ADC pin. Despite trying many different functions and code examples, we weren’t able to get consistent, satisfactory readings from the ADC pins. After several days of trying, we sought another solution.
By instead running the photo resistors to a capacitor, and wiring them to a GPIO pin, we were able to read the value of light hitting the photo resistor by first discharging the capacitor, then switching the GPIO pin to input mode and incrementing a counter until the capacitor is fully charged. By comparing the time it takes to charge the capacitor with each subsequent reading, we are able to detect sudden large changes in lighting and use this to trigger the trap. A doubling of the time it takes for the capacitor to charge would be enough to set off our trap.


//discharge capacitors

ioport_set_pin_dir(SERVO, IOPORT_DIR_OUTPUT);

ioport_set_pin_dir(PR1, IOPORT_DIR_OUTPUT);

ioport_set_pin_dir(PR2, IOPORT_DIR_OUTPUT);

ioport_set_pin_low( PR1);

ioport_set_pin_low( PR2);

delay_ms(5);

ioport_set_pin_dir(PR1, IOPORT_DIR_INPUT);

//increment counter to determine how long it takes to charge first capacitor

while(ioport_get_pin_level(PR1) != 1)

{

n1 = n1 + 1;

delay_ms(2);

}

&nbsp;

//increment counter to determine how long it takes to charge second capacitor

ioport_set_pin_dir(PR2, IOPORT_DIR_INPUT);

while(ioport_get_pin_level(PR2) != 1)

{

n2 = n2 + 1;

delay_ms(2);

}

&nbsp;

if (n1 > (o1*2) || n2 > (o2*2)) //executes if time to charge capacitor doubles from one reading to next.

{

pwm_set_duty_cycle_percent(&myservo[0],425); //turn servo to release bar, if trap is set

gfx_mono_draw_string("SNAP!          ", 0, 0, &sysfont);

gfx_mono_draw_string("SNAP!          ", 0, 12, &sysfont);

Leave a Reply

Your email address will not be published. Required fields are marked *