![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
part 6 and extra credit
Dependencies: C12832 Servo mbed
main.cpp@4:9c354b69b590, 2015-03-06 (annotated)
- Committer:
- jaredwil
- Date:
- Fri Mar 06 00:28:38 2015 +0000
- Revision:
- 4:9c354b69b590
- Parent:
- 3:853bc68793d0
new;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaredwil | 0:ba4c01e9484b | 1 | #include "mbed.h" |
jaredwil | 0:ba4c01e9484b | 2 | #include "C12832.h" |
jaredwil | 0:ba4c01e9484b | 3 | #include "Servo.h" |
jaredwil | 0:ba4c01e9484b | 4 | |
jaredwil | 0:ba4c01e9484b | 5 | |
jaredwil | 0:ba4c01e9484b | 6 | Servo s1(p21); |
jaredwil | 0:ba4c01e9484b | 7 | |
jaredwil | 0:ba4c01e9484b | 8 | C12832 lcd(p5, p7, p6, p8, p11); |
jaredwil | 0:ba4c01e9484b | 9 | |
jaredwil | 0:ba4c01e9484b | 10 | DigitalOut myled(LED1); |
jaredwil | 0:ba4c01e9484b | 11 | AnalogIn light(p16); |
jaredwil | 0:ba4c01e9484b | 12 | |
jaredwil | 0:ba4c01e9484b | 13 | int main() { |
jaredwil | 0:ba4c01e9484b | 14 | |
jaredwil | 0:ba4c01e9484b | 15 | float val,val2, count = 0; |
jaredwil | 4:9c354b69b590 | 16 | float pos = 0; |
jaredwil | 0:ba4c01e9484b | 17 | |
jaredwil | 1:016e2bcda058 | 18 | //Flash Phone Flash 4 times at close range to start the servo |
jaredwil | 1:016e2bcda058 | 19 | |
jaredwil | 0:ba4c01e9484b | 20 | while(count < 4){ |
jaredwil | 0:ba4c01e9484b | 21 | val = light.read(); |
jaredwil | 0:ba4c01e9484b | 22 | if(val<0.5){ |
jaredwil | 2:1d960bbbb0fe | 23 | lcd.locate(0,3); |
jaredwil | 2:1d960bbbb0fe | 24 | lcd.printf("reading: %.3f", val); |
jaredwil | 0:ba4c01e9484b | 25 | wait(0.1); |
jaredwil | 0:ba4c01e9484b | 26 | count++; |
jaredwil | 0:ba4c01e9484b | 27 | while(light.read() < 0.5); |
jaredwil | 3:853bc68793d0 | 28 | lcd.cls(); |
jaredwil | 0:ba4c01e9484b | 29 | } |
jaredwil | 0:ba4c01e9484b | 30 | |
jaredwil | 0:ba4c01e9484b | 31 | } |
jaredwil | 1:016e2bcda058 | 32 | |
jaredwil | 0:ba4c01e9484b | 33 | while(1) { |
jaredwil | 1:016e2bcda058 | 34 | lcd.cls(); |
jaredwil | 1:016e2bcda058 | 35 | val = light.read(); |
jaredwil | 2:1d960bbbb0fe | 36 | |
jaredwil | 1:016e2bcda058 | 37 | val2 = 1-val; |
jaredwil | 1:016e2bcda058 | 38 | |
jaredwil | 4:9c354b69b590 | 39 | |
jaredwil | 4:9c354b69b590 | 40 | /*application one |
jaredwil | 1:016e2bcda058 | 41 | //even for little light start servo with small steps(low speed) |
jaredwil | 1:016e2bcda058 | 42 | if(val2 > 0.1) { |
jaredwil | 2:1d960bbbb0fe | 43 | lcd.locate(0,3); |
jaredwil | 2:1d960bbbb0fe | 44 | lcd.printf("Sensor reading: %.3f", val); |
jaredwil | 1:016e2bcda058 | 45 | for(float p=0; p<1.0; p += val2) { |
jaredwil | 1:016e2bcda058 | 46 | s1 = p; |
jaredwil | 1:016e2bcda058 | 47 | wait(0.2); |
jaredwil | 1:016e2bcda058 | 48 | } |
jaredwil | 1:016e2bcda058 | 49 | for(float p=1.0; p>0; p -= val2) { |
jaredwil | 1:016e2bcda058 | 50 | s1 = p; |
jaredwil | 1:016e2bcda058 | 51 | wait(0.2); |
jaredwil | 1:016e2bcda058 | 52 | } |
jaredwil | 1:016e2bcda058 | 53 | } |
jaredwil | 4:9c354b69b590 | 54 | |
jaredwil | 4:9c354b69b590 | 55 | */ |
jaredwil | 4:9c354b69b590 | 56 | if(val2 > 0.1) { |
jaredwil | 4:9c354b69b590 | 57 | pos += val2*.05; |
jaredwil | 4:9c354b69b590 | 58 | |
jaredwil | 4:9c354b69b590 | 59 | if(pos > 1) |
jaredwil | 4:9c354b69b590 | 60 | pos -= 1; |
jaredwil | 4:9c354b69b590 | 61 | |
jaredwil | 4:9c354b69b590 | 62 | s1 = pos; |
jaredwil | 4:9c354b69b590 | 63 | wait_ms(10); |
jaredwil | 4:9c354b69b590 | 64 | } |
jaredwil | 4:9c354b69b590 | 65 | |
jaredwil | 0:ba4c01e9484b | 66 | } |
jaredwil | 0:ba4c01e9484b | 67 | } |