Show Posts
Pages: [1] 2 3 ... 9
1  Using Arduino / Programming Questions / Re: Where am I going wrong? on: September 10, 2011, 05:11:16 pm
You can't use delay() in an ISR because it relies on interrupts for timing and the interrupts are disabled during an ISR.

If you declare 'count' as volatile and just set it to 0 in the ISR then loop() will put the processor back to sleep in 10 seconds.

Code:
#include <avr/sleep.h>
#include <avr/power.h>

int fsr = 0;
volatile int count = 0;
int analogPin = 0;

[...]

ISR(ANALOG_COMP_vect)
{
  if(analogRead(analogPin) > 100)
  {
    count = 0;
    fsr = analogRead(analogPin);
    Serial.println("The reading is ");
    Serial.println(fsr);
  }
}

Hey, just wanted to let you know this worked a treat. Many thanks!!

Seán
2  Using Arduino / Programming Questions / Re: Where am I going wrong? on: September 09, 2011, 05:44:43 pm
Ah ok, that's handy! Thanks a lot! I'll report back on how I get on.

Thanks again!

Seán
3  Using Arduino / Programming Questions / Re: Where am I going wrong? on: September 09, 2011, 05:15:43 pm
Hey,

Thanks a lot! I'll give that a go when I get back home.

Can I ask, what does volatile mean in this context?

Thanks.

Seán
4  Using Arduino / Programming Questions / Where am I going wrong? on: September 09, 2011, 04:23:43 pm
Hello,

I hope someone can give me some advice ...I think I am just doing something silly with my code, but I can't see it.

I am able to put my system into sleep (Sleeping I) after ten seconds of inactivity. When it is in sleep mode, when I get a reading on the analog input it wakes up and takes a reading ....I want it to go back into sleep (Sleeping II) after more inactivity, but it isn't doing that. Where am I going wrong?

Also, when it wakes, I want it to take readings for as long as the pressure is applied, with 10ms gaps, but I can't get it to do that either.

Thanks in advance.

Seán

Code:
#include <avr/sleep.h>
#include <avr/power.h>

int fsr = 0;
int count = 0;
int analogPin = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(analogPin, INPUT);
 
  ACSR =
      (0<<ACD) |   //Comparator, Enabled
      (0<<ACBG) |  //AIN0 is applied to the positive input
      (0<<ACO) |   // Analog Comparator Output: Off
      (1<<ACI) |   //Clear Pending Interrupt
      (1<<ACIE) |  //Analog Comparator Interrupt, Enabled
      (0<<ACIC) |  //Analog Comparator Input Capture, Disabled
      (1<<ACIS1) | (1<ACIS0); //Capture on Rising Edge
}

void sleepNow()
{
  set_sleep_mode(SLEEP_MODE_IDLE);
  sleep_enable();    //enables the sleep mode bits in the register
 
  power_spi_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_twi_disable();
 
  sleep_mode();
  sleep_disable();
  power_all_enable();
}

void loop()
{
  count++;
  delay(1000);
  if(count >= 10)
  {
    Serial.println("Sleeping I");
    sleepNow();
  }
}

ISR(ANALOG_COMP_vect)
{
  if(analogRead(analogPin) > 100)
  {
    count = 0;
    fsr = analogRead(analogPin);
    Serial.println("The reading is ");
    Serial.println(fsr);
    delay(10);
    count = 0;
    if(count >= 10)
    {
      Serial.println("Sleeping II");
      sleepNow();
      count = 0;
    }
  }
}

5  Using Arduino / Programming Questions / Re: Analog Comparator on: September 08, 2011, 12:08:59 pm
Hi there,

Just have some more to add-on to this one.

I am wondering if this code makes sense -- I'm largely making it up as I go along. I want to put the system into sleep mode, after a specific amount of time, and wake up and when it detects an analog input, through the FSR.

Code:
#include <avr/sleep.h>
#include <avr/power.h>

int fsr = 0;
int count = 0;
int analogPin = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(analogPin, INPUT);
 
  ACSR =
      (0<<ACD) |   //Comparator, Enabled
      (0<<ACBG) |  //AIN0 is applied to the positive input
      (0<<ACO) |   // Analog Comparator Output: Off
      (1<<ACI) |   //Clear Pending Interrupt
      (1<<ACIE) |  //Analog Comparator Interrupt, Enabled
      (0<<ACIC) |  //Analog Comparator Input Capture, Disabled
      (1<<ACIS1) | (1<ACIS0); //Capture on Rising Edge
}

void sleepNow()
{
  set_sleep_mode(SLEEP_MODE_IDLE);
 
  sleep_enable();    //enables the sleep mode bits in the register
 
  power_spi_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_twi_disable();
 
  sleep_mode();
 
  sleep_disable();
 
  power_all_enable();
}

void loop()
{
  count++;
  delay(1000);
  if(count++ == 100)
  {
    Serial.print("Sleeping");
    sleepNow();
  }
}

ISR(ANALOG_COMP_vect)
{
  //Code to read from fsr
}

Thanks in advance.

Seán
6  Using Arduino / Programming Questions / Re: Analog Comparator on: September 05, 2011, 10:43:53 am
Cool!

Well, even just knowing where to connect into is a help! I can always try and figure the code out, and get help where I can.

Thanks again.

Seán
7  Using Arduino / Programming Questions / Re: Analog Comparator on: September 05, 2011, 10:38:27 am
To correct a previous post:  The +ve input of the comparator can be connected either to the internal 1.1V bandgap reference or to pin AIN0 (which is the same Arduino pin 6, _not_ A0).

The -ve input can be connected either to AIN1 (Arduino pin 7) or to the ADC multiplexor (and thus any of the analog inputs).

See section 22 of the datasheet for more details.

Thanks for the reply!

Pins 6 and 7 are the digital pins, yes?
And I take it you connect to the internal 1.1V bandgap reference through the code?

I have been reading the datasheet, but it can be a bit confusing ...to me anyway.

Thanks.

Seán
8  Using Arduino / Programming Questions / Re: Waking from sleep. on: September 05, 2011, 10:32:47 am
Depending on the level of the analog signal, you might also be able to use the analog comparator in the chip. See section 22 in the atmega328p datasheet.

Thanks for the reply ....yea, I am trying to figure out the analog comparator ...I put up a post about it for a littel help ...so, I will be giving that a good too.

Thanks for all the replies.

Seán
9  Using Arduino / Programming Questions / Waking from sleep. on: September 05, 2011, 06:00:23 am
Hello,

I have some code that will put my system to sleep, but I am not sure how take it out of sleep.

Do I need to attached an interrupt?

Ideally I would like it to wake up when it receives an analog signal in. This is why I am using the Idle mode. I believe that in idle, it can still receive analog signals. However, that may be incorrect.

Code:
#include <avr/power.h>
#include <avr/sleep.h>

int count = 0;
int analogPin = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(analogPin, INPUT);
}

void sleepNow()
{
  set_sleep_mode(SLEEP_MODE_IDLE);
 
  sleep_enable();    //enables the sleep mode bits in the register
 
  power_spi_disable();
  power_timer0_disable();
  power_timer1_disable();
  power_timer2_disable();
  power_twi_disable();
 
  sleep_mode();
 
  sleep_disable();
 
  power_all_enable();
}

void loop()
{
  count++;
  delay(1000);
 
  if(analogRead(analogPin) < 10 && count == 10)
    {
      Serial.print("Sleeping");
      delay(100);  //Delay needed to avoid error
      //count = 0;
      sleepNow();
    }
    if(analogRead(analogPin) > 11)
    {
      Serial.println("Hello again");
      delay(100);
      count = 0;
  }
}
10  Using Arduino / Programming Questions / Re: Analog Comparator on: September 05, 2011, 05:55:03 am
Hey,

Thanks for the reply! I will read over the post you suggest.

I am trying a few different methods for saving power, I'm just not sure what way to go.

But thanks again!!

Seán
11  Using Arduino / Programming Questions / Analog Comparator on: September 04, 2011, 03:42:24 pm
Dia duit mo chairde,

I am trying to set up the analog comparator, but I would just like to ask a few basic questions about it first.

I will be using a reference voltage of about 0.6v, but do I need to specify which pin I am using as the reference?

Also, the way this works is ...when the system is below the reference voltage, it does not jumo to an ISR, but when it goes above the reference voltage, it does go to the ISR?

It can't be that simple ...can it?

Thanks,

Seán
12  Using Arduino / Project Guidance / Re: Some advice? on: September 01, 2011, 04:47:49 pm
Hey,

I am using the ADXL335.

I don't think it has any of the features you talk about, but I will have a look.

Seán
13  Using Arduino / Project Guidance / Re: Some advice? on: September 01, 2011, 09:59:04 am

Thanks! I will read through this.

Seán
14  Using Arduino / Project Guidance / Re: Some advice? on: September 01, 2011, 09:57:06 am
My instinct is that you will want an analogue comparator to watch the signal from the accelerometer and generate an interrupt signal on the Arduino if it crosses a certain threshold.

Yes, that's it.

Is this a reasonable way to do what I want? Or is there a different path?

Seán
15  Using Arduino / Project Guidance / Some advice? on: September 01, 2011, 08:57:43 am
Hello,

I am hoping someone can give me some advice on a path forward.

I need to put my system into sleep for long persions of time, or an idle mode. When I receive a signal from an accelerometer, I want the system to wake up and start taking in data.

I was initially going to use the WDT, wakening up every 500ms or so, but there are a few issues with this
  • The system will not be taking data in for long periods of time -- it will probably only take in data for say, 6-10 of the 26 hours in a day, so waking every 500ms is still wasting a lot of power.
  • Even waking every 500ms, I will miss the start of some events, and will not have complete data

So, I have been talking to a few people, and reading the datasheet for the ATmega328P, and I am wondering would the analog comparator be useful? My thinking is that I can set up an analog reference, below which the system wills stay idle, but above it (i.e when the voltage on AIN0 > AIN1) the output takes the system out of idle, and the system goes on as normal.

Does this make sense? Or am I just being silly?

If I am not being silly, this brings me to my second issue ...I don't have a fracking clue how to do it. I have been reading the datasheet, but if someone has some sort of example, that would be great!

I did find this, which gives some insight - http://arduino.cc/forum/index.php/topic,59804.0.html ...but I am wondering if anyone has any others?

Thanks in advance.

Seán
Pages: [1] 2 3 ... 9