Show Posts
Pages: [1] 2 3 ... 1307
1  Using Arduino / Programming Questions / Re: MersenneTwister.h Troubleshooting#size on: Today at 02:40:19 am
Yes, very nice.

Code:
/* Implementation of a 32-bit KISS generator which uses no multiply instructions */
static unsigned int x=123456789,y=234567891,z=345678912,w=456789123,c=0;
unsigned int JKISS32()
{
  int t;
  y ^= y << 5;
  y ^= y >> 7;
  y ^= y << 22;
  t = z + w + c;
  z = w;
  c = t < 0;
  w = t & 2147483647;
  x += 1411392427;
  return x + y + w;
}

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println ("Starting ...");
  }  // end of setup

const int ITERATIONS = 20000;
volatile unsigned int result;

void loop ()
  {
  unsigned long start, finish;
  int i;
 
  start = micros ();
  for (i = 0; i < ITERATIONS; i++)
    result = JKISS32 ();
  finish = micros ();
   
  Serial.print ("JKISS32 took ");
  Serial.print (finish - start);
  Serial.println (" uS");
 
  start = micros ();
  for (i = 0; i < ITERATIONS; i++)
    result = rand ();
  finish = micros ();
   
  Serial.print ("rand took ");
  Serial.print (finish - start);
  Serial.println (" uS");
 
  }  // end of loop


Code:
Starting ...
JKISS32 took 141556 uS
rand took 1895996 uS


So the JKISS32 function took about 7.5% of the rand function time.

2  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: Today at 02:29:19 am
You can attach code to posts.

How to use this forum
3  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: Today at 12:49:58 am
One problem with this code is that you need to clear the WDT reset flag before you can disable the watchdog timer. I ran into this exact same issue with an ATtiny84V. Add MCUSR = 0; before wdt_disable();.

And put the watchdog disabling code right at the beginning of the setup().

I tried that and sadly it didn't work, eg.

Code:
#include <avr/wdt.h>

void setup()
{   
  MCUSR = 0;
  wdt_disable();
  wdt_enable(WDTO_4S);
  Serial.begin(115200);
  wdt_reset();
  Serial.println("Begin");
  Serial.flush ();
}

void loop()
{
  wdt_reset();
  cli();
  while (1)
  {
  }
}

According to a page I found ( http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html ) if the watchdog was enabled it still is on reset with a fast prescaler (15 mS). So if the bootloader doesn't turn it off you are in trouble. I tested with the Lilypad bootloader and it didn't work. With the Optiboot loader, it does work. So the problem is indeed in the bootloader. Personally I don't know where to find a fixed 8 MHz bootloader.
4  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: Today at 12:35:09 am
I'll have to add ... gasp! ... delay().

In case anyone is wondering how to do this (it is not totally trivial) here is what worked. I needed to take a line like this:

Code:
Keyboard.println (F("http://www.gammon.com.au/interrupts"));

And have it delay for (say) 5 milliseconds between each character. The easiest way I could find was to derive a class from Print, and use that. eg.

Code:
class my_Keyboard_class : public Print
{
public:
virtual size_t  write(uint8_t c);
};

size_t my_Keyboard_class::write (uint8_t character)
{
  Keyboard.write (character);
  delay (5);
  return 1;
}

my_Keyboard_class myKeyboard;

Now just change Keyboard.print (or println) to myKeyboard.print throughout the code and you are done. eg.

Code:
myKeyboard.println (F("http://www.gammon.com.au/interrupts"));
5  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: Today at 12:17:20 am
Thanks, I've been noticing my Arduino Leonardo "message generator" has been less reliable now I moved to Ubuntu. I'll have to add ... gasp! ... delay().

6  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: Today at 12:06:17 am
Quote
Code:
Serial.println("Begin");
...
cli();

One problem with your original code is that turning interrupts off stops serial output. That is why I had Serial.flush() in my code.
7  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: August 29, 2014, 11:58:25 pm
This version prints to serial every four seconds:

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

// watchdog interrupt
ISR (WDT_vect)
{
  wdt_disable();  // disable watchdog
}  // end of WDT_vect

void setup ()
{
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);
  Serial.begin (115200);
}  // end of setup

long counter = 0;

void loop()
{
  // sleep bit patterns:
  //  1 second:  0b000110
  //  2 seconds: 0b000111
  //  4 seconds: 0b100000
  //  8 seconds: 0b100001

  // sleep for 4 seconds
  const byte interval = 0b100000;
  
  MCUSR = 0;                          // reset various flags
  WDTCSR |= 0b00011000;               // see docs, set WDCE, WDE
  WDTCSR =  0b01000000 | interval;    // set WDIE, and appropriate delay
  wdt_reset();                        // pat the dog
  sleep_mode ();                      // now goes to sleep and waits for the watchdog

  Serial.print (F("Hello, world! Iteration: "));
  Serial.println (++counter);
  Serial.flush ();  // wait for output to complete
}  // end of loop
8  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: August 29, 2014, 11:54:40 pm
Rather than trying to reset the processor, generate an interrupt:

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

#define LED 13

// watchdog interrupt
ISR (WDT_vect)
{
  wdt_disable();  // disable watchdog
}  // end of WDT_vect

void setup ()
{
  pinMode (LED, OUTPUT);
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);
}  // end of setup

void loop()
{
  // sleep bit patterns:
  //  1 second:  0b000110
  //  2 seconds: 0b000111
  //  4 seconds: 0b100000
  //  8 seconds: 0b100001

  // sleep for 4 seconds
  const byte interval = 0b100000;
  
  MCUSR = 0;                          // reset various flags
  WDTCSR |= 0b00011000;               // see docs, set WDCE, WDE
  WDTCSR =  0b01000000 | interval;    // set WDIE, and appropriate delay
  wdt_reset();                        // pat the dog
  sleep_mode ();                      // now goes to sleep and waits for the watchdog

  // now do stuff
  for (byte i = 0; i < 5; i++)
    {
    digitalWrite (LED, HIGH);
    delay (250);
    digitalWrite (LED, LOW);
    delay (250);
    }

}  // end of loop

The above code flashes the LED on pin 13 every four seconds.

http://www.gammon.com.au/interrupts
9  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: August 29, 2014, 11:04:57 pm
I think you can do it if you take the right steps.

  • Power off the chip
  • Hold down the reset button
  • Power the chip back on, still holding reset down
  • Start uploading the sketch
  • Release the reset button

Doing it this way stops the sketch from running on power-up, and thus whatever-it-does doesn't matter. You go straight to the bootloader.

If you are running out of enough hands to do this you might use a wire with an alligator clip to keep reset shorted to ground, while you do the other steps, and then release it at the right moment.
10  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 10:00:57 pm
There is a fairly well-known issue with the Wire library where, if a certain interrupt does not arrive, it hangs indefinitely. This might be what is happening to you. On the link above (my I2C page) there is a link to another library that times out in this situation. You might want to try that.

I personally have not used that library, and I have a temperature sensor running all day every day (logging to an SD card) and it never hangs. So there may be an electrical issue with your circuit that is causing this to happen. For example, inadequate I2C pull-up resistors, or maybe just noise in the circuit (or inadequate decoupling).
11  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 09:35:34 pm
Just as a style thing, can you reformat the code a bit? The IDE has a Tools -> Auto Format feature.

Code:
...
   Serial.print("   ...deltaP: ");
    Serial.println(ambientPressure-BMP085.pressure);
   if(abs(BMP085.pressure-ambientPressure)>PRESSURE_TOLERANCE)
   {
    break; //exit if sample pump starts to pull a vaccum
   }
   elapsedTime=now()-startTime;
   delay(1000);
    }
 
    analogWrite(SAMPLE_PUMP_PIN,0); 
    delay(2000); 
   EvacLung();
   delay(1000);
 
  }void loop()
  {
    BMP085.refresh();
    ambientPressure=BMP085.pressure;
    startTime=now();
    elapsedTime=now()-startTime;
...

That is all a bit of a mess, particularly this line:

Code:
  }void loop()

Don't have the curly braces from one function on the same line as the start of a new function.



I'm also not keen on this "soft reset" stuff:

Code:
void soft_reset() //software reset; bring script to beggining of reset loop; internal watchdog on atmega
  {
    asm volatile ("  jmp 0");
  }

This doesn't reset the hardware you know, so the results may be a bit unpredictable.



Also these "magic numbers":

Code:
       for(int j=0;j<=5;j++)
        {
         Serial.print(comma);
         Serial.print(analogRead(Sensor[j]));
        }
 

We have to keep going back to the start of the code to see if Sensor is indeed declared as an array of six. Better is to make it explicit:

Instead of:

Code:
int Sensor[]={A15,A14,A13,A10,A11,A12};

Make it:

Code:
const int NUMBER_OF_SENSORS = 6;
int Sensor[NUMBER_OF_SENSORS]={A15,A14,A13,A10,A11,A12};

Now you can use that number in your loops instead of hard-coding "5" everywhere:

Code:
       
for(int j=0; j< NUMBER_OF_SENSORS; j++)
        {
         Serial.print(comma);
         Serial.print(analogRead(Sensor[j]));
        }
 

(I changed <= to < to get the right number of iterations).



You could check the status from endTransmission. You are getting the status but ignoring it:

Code:
  Wire.beginTransmission(SHT2x_address);
  Wire.write(temp_command);
  check1=Wire.endTransmission();

The variable check1 (which you are ignoring) will be zero if the transmission went OK.

More info:

http://www.gammon.com.au/i2c



You could check that you are getting back the number of bytes you expect:

Code:
  Wire.requestFrom(SHT2x_address, 3);
 
  TEMP_byte1=Wire.read();
  TEMP_byte2=Wire.read();
  TEMP_byte3=Wire.read();

Should really be:

Code:
  if (Wire.requestFrom(SHT2x_address, 3) != 3)
    {
   // handle the error
    }
  else
    {
    TEMP_byte1=Wire.read();
    TEMP_byte2=Wire.read();
    TEMP_byte3=Wire.read();
    }
12  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 09:19:36 pm
Aren't the strings in the Serial.print function loaded into the stack when the code is compiled and run for the first loop (in which case the code would have crashed during the first void loop(){} run)?

When you use the F macro that does not happen. In fact it never happens. Without the F macro strings are copied from program memory into RAM at program startup (not the stack, although the stack is in RAM), which can potentially fill up RAM. If you use the F macro they are pulled directly from program memory when they are needed.
13  Using Arduino / Programming Questions / Re: MersenneTwister.h Troubleshooting#size on: August 29, 2014, 07:39:35 pm
I suggest another random number generator that isn't so memory intensive. After all, even if changing to ints "worked", you have changed the whole way the algorithm works, and it may now be less powerful than other ones. For example these lines:

Code:
    /* Tempering */
    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);

They would be meaningless if you are dealing in 2-byte ints rather than 4-byte longs.
14  International / Portugues / Re: Função loop dentro do void loop() on: August 29, 2014, 07:30:33 pm
Nós moderadores têm acesso ao Google Translate, apesar de que pode dar traduções infelizes de termos técnicos.

Traduz Google reconhece palavrões e, portanto, eu aconselho membros de todas as partes do fórum para comportar corretamente.

(via Google Translate)
15  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 07:10:25 pm
Quote
Code:
        Serial.print("Elapsed Time: ");

You may be running out of memory. Try changing such lines to be:

Code:
        Serial.print(F("Elapsed Time: "));
Pages: [1] 2 3 ... 1307