Go Down

Topic: Put arduino nano to sleep and clumsy codes (Read 310 times) previous topic - next topic

henryvii99

Dear all,

I am very new to programming, and with the help from you guys,
with valuable advices from experts,  some cut and paste and luck,
I created an proximity sensor which does the following:

1. When sensors detect something near, it starts to vibrate for 3 seconds and stops.
2. When it is going to crash, motor vibrates vigorously for 3 seconds and stops.
3. When nothing in front of the sensor, it cause a delay of 500ms.
4. An additional LED signal to warn about imminent crash.

Code: [Select]
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
#define motorPin 3
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int motorState = 0;             // motorState used to set the LED
unsigned long previousMillis;        // will store last time LED was updated
unsigned long startTime;
long interval = 1000;
boolean lastTrip;
boolean lastTrip2;
//
int maximumRange = 200; // Maximum range needed
int minimumRange = 10; // Minimum range needed
int triggerDistance = 10;
int crashDistance = 5;
//long duration, distance; // Duration used to calculate distance
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
 pinMode(motorPin, OUTPUT);
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
long getDistance()
{
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 long duration = pulseIn(echoPin, HIGH);
 long dist = duration/58.2;
 return dist;
}
//////////////////////////////////////////////////////////////////////////////////
void runMotor()
{
 if( millis() - startTime < 3000) // Three Second Interval
 {
   analogWrite(motorPin, 25);
   //or digitalWrite(motorPin, HIGH);

 }
 else
 {
   analogWrite(motorPin, 0);
   //or digitalWrite(motorPin, LOW);
   motorState = 0;
 }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void runMotor2()
{
 if( millis() - startTime < 3000) // Three Second Interval
 {
   analogWrite(motorPin, 200);
   //or digitalWrite(motorPin, HIGH);

 }
 else
 {
   analogWrite(motorPin, 0);
   //or digitalWrite(motorPin, LOW);
   motorState = 0;
 }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
 long distance = getDistance();
 if (distance > minimumRange)
 {
   Serial.println(distance);
   digitalWrite(LEDPin, LOW);
 }
 if (distance < minimumRange)
 {
   Serial.println(distance);
   digitalWrite(LEDPin, HIGH);
 }
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 boolean tripped = (distance <= triggerDistance ? 1 : 0); // flag if triggered
 if (tripped)
 {
   if (tripped != lastTrip)
   {
     startTime = millis();
     motorState = 1;
   }
 }
 lastTrip = tripped;
 if (motorState == 1)
 {
   runMotor();
 }
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 boolean tripped2 = (distance <= crashDistance ? 1 : 0); // flag if triggered
 if (tripped2)
 {
   if (tripped2 != lastTrip2)
   {
     startTime = millis();
     motorState = 2;
   }
 }
 lastTrip = tripped;
 if (motorState == 2)
 {
   runMotor2();
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////
if (distance > triggerDistance)
{analogWrite(motorPin, 0);
motorState = 0;
}
delay (500);
}


While it does fit my needs, I want to reduce the power consumption.

First of all, I would like to put my arduino nano to sleep instead of simply delaying.
From this blog, it looks like I can integrate the codes, but I don't understand the hex numbers.

It want to sleep for 500ms, not 4.1sec. How should I modify the  TCCR1A = 0x00 value?

How can I turn off Arduino nano LED? It seems that it just eats away my battery for no reasons.

Lastly, I think my codes is quite clumsy. I think there is a way to shorten it.
Any demonstrations are appreciated, and many thanks in advance!

BulldogLowell

#1
Aug 04, 2014, 04:44 pm Last Edit: Aug 04, 2014, 04:46 pm by BulldogLowell Reason: 1
putting the arduino to sleep means you won't be detecting any distance...  Is that what you want?
also, you will need to power down the sensor, which is probably using the most current along with the motors, I would think.

For example, if you were to develop a garage proximity sensor that indicates the distance from a vehicle entering, you could wake up the arduino with an interrupt attached to a switch on the door.  When the door opens, wake the arduino and start sensing and displaying the distance, then sleep when the distance was unchanged for a longish period of time.

I cannot comment on the 'clumsy' code, other than to say delete all of the superfluous commentary and use auto format to make it look more pretty...  :smiley-red:

there are instructions on the web on cutting the power to the on-board LEDs with an X-Acto blade; be careful.

added,

BTW, you could pass variables to a single function for the motor number and time to reduce the functions by one.

CrossRoads

You can put the 328 to sleep. Won't help with the FTDI chip, the voltage regulator, or the power on LED, the motor driver, the other sensor, as noted. What you really need is a standalone baord that runs from 3V or 4.5V battery, or 3.7V LiPo/LiIon, with those components left off, and with high side switch(es) to power down the other stuff.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

henryvii99


You can put the 328 to sleep. Won't help with the FTDI chip, the voltage regulator, or the power on LED, the motor driver, the other sensor, as noted. What you really need is a standalone baord that runs from 3V or 4.5V battery, or 3.7V LiPo/LiIon, with those components left off, and with high side switch(es) to power down the other stuff.


Many thanks for your reply.

Does that mean:

I can put some parts in Arduino (328 chip) to sleep, reducing power consumption, but

FTDI chip, voltage regulator and LED will drain my power for no reasons?

And the most simple solution is to use a chip without FTDI, voltage regulator and LED? Like Lilypad Arduino?

CrossRoads

Yes.
I don't know about Lilypad, too many variations. Perhaps.
Looks like even the barest has  a charge control chip.
Maybe Solarbotics ardweeny
https://solarbotics.com/product/kardw/
leave off the power LED, run on 3 AA battery.

Or my SMD version, with Rs, Cs xtal on the bottom. Connectorize as needed.


Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

henryvii99

Thank you.

I would like to know, what is the power consumption for each component on Arduino nano?

Theoretical my sensor can run for a week, given nano taking20mA and ultrasonic takes 15mA, using a 6000mAh power bank.
If I  can cut power consumption by half, I can make sure it can run more than a week and charge along with my wheelchair.

CrossRoads

Have to look up datasheet for FTDI chip, regulator, measure the voltate across the power LED resistor, etc.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

henryvii99


Have to look up datasheet for FTDI chip, regulator, measure the voltate across the power LED resistor, etc.


After searching the internet, I have found this blog:

http://www.ba0sh1.com/project-crystal-part-1/

From his experiment, it seems that by putting arduino to sleep and wake up on external interrupt for sensor reading,
the power consumption of Arduino nano and sensor reduces from 37mA to 2mA.

As for my project, I don't really need it to run the sensor for months, a 50% increase of battery life would be sufficient.
In this case, do you think a snooze of 500ms (instead of a 500ms delay)  for every sensor read can save some power?

Anyway, as far as I know, delay function cease code running, do I even have to include a sleep function?

I found the sleep codes here:

http://donalmorrissey.blogspot.hk/2011/11/sleeping-arduino-part-4-wake-up-via.html

And I think modify the TCNT1=0x0000 to 0xDFF6 can make the sleep cycle from ~4s down to ~500ms?

CrossRoads

delay does not cease code running - processor is still cranking away at 16 MHz, it's just not doing anything but counting the time going by.
Power down sleep mode vs sitting in delay will definitely save on power.
I don't know about the durations. I made an RF remote control using a promini powered from LiPo battery. Pressing a button created an external interrupt to wake it up, read the keypad & send the button press, then go back into power down sleep mode. Runs a month or so on 1000mAH LiPo, just sitting there with no button presses. I should rebuild it into a standalone module with no power LED, no regulator, and p-channel mosfet to ensure the transmitter is all the way off.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up