Hey all, I'm working on a model drawbridge project using two servo motors and I'm having an issue trying to delay two servo motors when it reaches a certain y-axis peak read by my microprocessor'a LED display. Let me walk through the code that's written. When the LED display reads "50" the motors turn on and begins to wind up the bridge. When the bridge reaches its max height (LED display reads >95), the bridge automatically draws back down to its original position and stops when the LED displays ("53"). So my main problem is trying to write out a code so the motors will delay for a few seconds after it reaches its max peak of "95" before starting up again to draw the bridge back down.
Any ideas or tips is greatly appreciative! Thanks.
Any ideas or tips is greatly appreciative! Thanks.
#include <stdio.h>
#include <stdlib.h>
#include "io.hpp" //IO board
#include "utilities.h" //delay_ms()
#include "LPC17xx.h"
#include "lpc_pwm.hpp"
float ascalc(float axis, float max);
int main(void)
{
while (1){
LE.off(8);
printf("Switch four is pressed\n");
LE.on(4);
int Yaxis=AS.getY(); // Replace this WHOLE code with a function!!!
int percent= ascalc(Yaxis,2048); //THIS IS THE Y AXIS CODE
LD.setNumber(percent);
// delay_ms(100);
if(percent==50) //bridge goes up
{
while(1){
//Beginning of new code; THIS CONTROLS THE MOTOR
printf("bridge moves up.\n");
LE.on(1);
PWM pwm(PWM::pwm2, 50);
pwm.set(10);
LE.on(8);
if(percent==50){
PWM pwm(PWM::pwm4, 50);
pwm.set(5);
int Yaxis=AS.getY();
int percent= ascalc(Yaxis,2048); //THIS IS THE Y AXIS CODE
LD.setNumber(percent);
break;
}
}
}
//Need some sort of delay HERE to allow bridge to remain up for a few seconds before going back down
if(percent>95 ) //bridge goes down
{
while (1){
int Yaxis=AS.getY();
int percent= ascalc(Yaxis,2048); //THIS IS THE Y AXIS CODE
LD.setNumber(percent);
PWM pwm(PWM::pwm2, 50);
//delay_ms(1000);
pwm.set(5);
delay_ms(100); //DELAY
if(percent>95){
PWM pwm(PWM::pwm4, 50);
//delay_ms(1000);
pwm.set(10);
delay_ms(100); //DELAY
}
if(percent<=53) //bridge stops, servo motors are now off
{
while(1){
int Yaxis=AS.getY();
int percent= ascalc(Yaxis,2048); //THIS IS THE Y AXIS CODE
LD.setNumber(percent);
PWM pwm(PWM::pwm2, 50);
pwm.set(0);
//delay_ms(100); //DELAY
if(percent<=53){
PWM pwm(PWM::pwm4, 50);
pwm.set(0);
}
}
}
}
}
}
return (0);
}
float ascalc(float axis, float max)
{
return(axis+1024)/max*100;
}