Saturday, March 12, 2011

No Delay

I am at present working on the communications system for the epic boat and decided that having the functions with delays was not a great way to deal with code execution and certinly instead of having the processor sitting there doing nothing let it go off and process other things then come back after its done that.

 //Timed Loops with no delays. for use with arduino
#define CLOCK_TICKS
#define LOOP_TICKS
#define A_LED_PIN 11  //36 = B, 37 = A, 35 = C
#define B_LED_PIN 10
unsigned long func_tick_0;
unsigned long func_tick_1;
long func_0 = 10000000; // how long to wait before running func0 1000 microseconds = 1ms
long func_1 = 20000;

int led_A = 0;       //State of the LED's 0=On 1=Off
int led_B = 0;
unsigned long tick(){
  unsigned long ticks;
  //it may need something to check for clock rollover
  ticks = micros();
  return ticks;
}
int toggle_led(int ledPin, int ledstate){
if(ledstate==0){
  digitalWrite(ledPin, HIGH);   // sets the LED on
  ledstate=1;
}else{
  digitalWrite(ledPin, LOW);   // sets the LED on
  ledstate=0;
}
return ledstate;
}
void setup(){
  //Serial.begin(57600); // opens serial port, sets data rate to 9600 bps
  pinMode(A_LED_PIN, OUTPUT);      // sets the digital pin as output
  pinMode(B_LED_PIN, OUTPUT);      // sets the digital pin as output
  digitalWrite(A_LED_PIN, LOW);   // sets the LED on
  digitalWrite(B_LED_PIN, LOW);   // sets the LED on
}
void loop(){
  
  if(tick() >= func_tick_0 + func_0){
    led_A = toggle_led(A_LED_PIN, led_A); //returns the state of the LED
    func_tick_0 = tick();
  }
 
    if(tick() >= func_tick_1 + func_1){
    led_B = toggle_led(B_LED_PIN, led_B); //returns the state of the LED
    func_tick_1 = tick();
  }
}

No comments:

Post a Comment