Jump to content
43oh

newbee. difference between sleep() delayMicroseconds()


Recommended Posts

I am taking an intro to electronics course. In lab we are implementing "power blocking" to reduce the total amount of power used by our sensor front end. Bellow is the sketch we are using. I do not understand how it works. The way another student described the code to me is: assume the entire loops runs in 1 + 19 ms. power would be on for a total of 1/20 ms == 5% of the time

 

 

my confusion is with the call delayMicroseconds(1000)  in the loop. (I did not find the documentation helpful)

 

thanks in advance

 

Andy

 

 

//***************************************************************************************
//  EE40LX
//  Sketch 3.2
//
//  Description; Power-block a 3.3V rail at P1.1 and subsequently read inputs from
//     Wheatstone bridges, connected to P1.2 and P1.7
//
//  Tom Zajdel
//  University of California, Berkeley
//  July 27, 2015
//
//  Version 1.2 July 27, 2015 - Added curly brackets to conditional statements
//
//  Version 1.1 January 26, 2015 - Fixed a timing bug by using delayMicroseconds()
//  and also corrected errors in pin assignment
//
//***************************************************************************************
 
int PBRAIL = P1_1;   // set PBRAIL as P1.1 alias
int LPHOTO = P1_2;   // set LPHOTO as P1.2 alias
int RPHOTO = P1_7;   // set RPHOTO as P1.7 alias
 
int REDLED = P1_0;   // set REDLED as P1_0 alias
int GRNLED = P1_6;   // set GRNLED as P1_6 alias
 
void setup()
  
  // set power block pin and led pins as outputs
  pinMode(PBRAIL, OUTPUT);
  pinMode(REDLED, OUTPUT);
  pinMode(GRNLED, OUTPUT);
  
  // set photocell input pins
  pinMode(LPHOTO, INPUT);
  pinMode(RPHOTO, INPUT);
 
}
 
void loop()
{
  digitalWrite(PBRAIL, HIGH);        // supply 3.3V to the power rail
  delayMicroseconds(1000);           // delay briefly to allow comparator outputs to settle
 
  if (digitalRead(LPHOTO) == HIGH)  // if LPHOTO is on, turn REDLED on
  {
     digitalWrite(REDLED, HIGH);     // otherwise, turn REDLED off
  }
  else
  {
     digitalWrite(REDLED, LOW);  
  }   
  if (digitalRead(RPHOTO) == HIGH)   // if RPHOTO is on, turn GRNLED on
  {
     digitalWrite(GRNLED, HIGH);     // otherwise, turn GRNLED off
  }
  else
  {
     digitalWrite(GRNLED, LOW);  
  }
  
  digitalWrite(PBRAIL, LOW);          // turn the power rail off again
  sleep(19);                          // wait 19 ms (can do other tasks in this time,
                                      // but we are simply demonstrating that you can cut power
                                      // to the circuits for 95% of the time and not notice!
 
}
Link to post
Share on other sites

@@AEDWIP

 

  • 1000 microseconds are equivalent to 1 millisecond
  • sleep() is a low power mode that takes milliseconds as an argument
  • so delayMicroseconds(1000) counts for 1 millisecond while power is on and then sleep(19) has things wait for 19 milliseconds with it off
  • and the power to P1_1 is on only approximately 5% of the time
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...