.

Dependencies:   Servo mbed

main.cpp

Committer:
ericoneill
Date:
2015-02-27
Revision:
2:30ebae0d3e17
Parent:
1:8e5821dec0f7
Child:
3:7eaf505f811e

File content as of revision 2:30ebae0d3e17:

#include "mbed.h"

DigitalOut myled(LED1);
PwmOut servo(PTA5);
PwmOut motor(PTA4);
Serial pc(USBTX, USBRX); // tx, rx
DigitalIn din(PTA13);
Timer t;

void motor_sweep() {
    for(float p = 0.0; p<.0025; p+=.0001){
        motor.pulsewidth(p);
        if(p == 0.0 || p == 1.0 || p == .0007){
            wait(2);
        }
        wait(.1);
    }
}

void servo_sweep(){
    for(float p = 0.001; p<0.002; p+=0.0001){
            servo.pulsewidth(p);
            wait(0.5);
    }
}

int main() {
    servo.period(0.005);
    motor.period(.0025);
    int previous_val = -1;
    
    t.start();
    int lastchange = 0;
    while(1){
        if(din) {
            if(previous_val != 1){
                int current_time = t.read_ms();
                int interval = current_time - lastchange;
                lastchange = current_time;
                pc.printf("light to dark time : %d\n\r", interval);
                previous_val = 1;
            }
            myled = 1;
            //pc.printf("dark");
        } else {
            if(previous_val != 0){
                int current_time = t.read_ms();
                int interval = current_time - lastchange;
                lastchange = current_time;
                pc.printf("dark to light time : %d\n\r", interval);
                previous_val = 0;
            }
            myled = 0;
            //pc.printf("light");
        }
        wait(.2f);
        
        char choice = pc.getc();
        pc.putc(choice);
        switch(choice){
            case '0':
                motor.pulsewidth(0.0);
                pc.printf("0% \n\r");
                break;
            case '1':
                motor.pulsewidth(.0025);
                pc.printf("100% \n\r");
                break;
            case '3':
                motor.pulsewidth(.0025*.3);
                pc.printf("30% \n\r");
                break;
            case '5':
                motor.pulsewidth(.0025*.5);
                pc.printf("50% \n\r");
                break;
            default:
                motor.pulsewidth(.0025*.3);
                pc.printf("default\n\r");
                break;
        }
        
       //servo_sweep();
       //motor_sweep();
       //motor.pulsewidth(.0025);
    }
}