Go Down

Topic: RAM RAM I need more RAM!! (Read 3 times) previous topic - next topic

jimLee

#20
Apr 02, 2012, 08:59 pm Last Edit: Apr 02, 2012, 09:12 pm by jimLee Reason: 1
blinker.h

Code: [Select]


#ifndef blinker_h
#define blinker_h

// A simple blink class used to turn on and off blinking LEDs.
// Spawn a blinker for each pin you would like to blink an LED on.
// Its not ment to be the end all. Its just something to blink LEDs.
// It will probably crash and burn when the microsecond counter overruns.
//
// Written by : Jim Lee @ www.LeftCoast.biz


// Some defaults in case the user just doesn't care..
#define defPin 13
#define defOnMs 50
#define defPeriodMs 400

class blinker {

public:
blinker(int inPin=defPin,float inOnMs=defOnMs, float inPeriodMs=defPeriodMs);

void init(bool running=true);         // Call this in the setup function perhaps?
void setBlink(bool onOff); // Start or stop the blinking..
void idle(void); // Call this in the loop function.

void setTimes(float inOnMs, float inPeriodMs); // Want to change the blink?

private:
unsigned long onTime;
unsigned long periodTime;
unsigned long startTime;
bool running;
bool pinHigh;
int pin;
};

#endif



blinker.cpp

Code: [Select]

#include "blinker.h"
#include <arduino.h>

blinker::blinker(int inPin,float inOnMs, float inPeriodMs) {

   running = false;
   pinHigh = false;
   pin = inPin;
   setTimes(inOnMs,inPeriodMs);
}


void blinker::init(bool running) {

   pinMode(pin, OUTPUT);
   setBlink(running);
}


void blinker::setBlink(bool onOff) {

   if(onOff != running) { // ignore if no change
      if (onOff) { // Start blinking..
         startTime = micros(); // Starting NOW!
         digitalWrite(pin,HIGH); // light on!
         pinHigh = true; // set state.
         running = true;
      }
      else {          // Stop blinking..
         digitalWrite(pin,LOW); // light off.
         running = false; // set state.
         pinHigh = false;
      }
   }
}


void blinker::idle(void) {

   unsigned long now;

   if (running) {
      now = micros(); // Look at the time!
      if (pinHigh) { // If the light is on..
         if (now >= startTime+onTime) { // And its been on long enough!
            digitalWrite(pin,LOW); // light off.
            pinHigh = false; // set state
         }
      }
      else { // Else the light is off..
         if (now >= startTime+periodTime) { // And its been off long enough!
            digitalWrite(pin,HIGH); // light on!
            pinHigh = true; // set state
            startTime = startTime + periodTime; // Set next iteration.
         }
      }
   }
}


void blinker::setTimes(float inOnMs, float inPeriodMs) {

   onTime = round(1000*inOnMs);
   periodTime = round(1000*inPeriodMs);
}



I think that's all of it..

-jim lee

smeezekitty

Holy cow! I can see why you are running out of ram.
But, there is room for optimizations.
First of all, some of the instructional strings in main.cpp can be stored in PROGMEM. That should gain you atleast 100 bytes.
I am also sure there are some optimizations that can be done in the "thorax" code but it makes my brain hurt to look at.  :smiley-roll:
Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

AWOL

Lots of candidates for PROGMEM strings there
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GoForSmoke

Hey Jim,
Really you should look into reframicating the software if only that others may use it on UNO's buuuut,

Check out the Teensy++!
http://www.pjrc.com/teensy/index.html
-----------------------------------------------------
128k flash, 8k SRAM, 4k EEPROM.
46 I/O pins; 8 analog inputs, 9 PWM's.
USB based Atmel chip, programmable as HID/MIDI and they have libraries.
Really small. Arduino compatible, able to use the Arduino IDE.
Way more than an UNO, not sure how it rates compared to a MEGA2560.
With pins (sticks right into a breadboard), $27. Without, $24.

Or you can buy SPI-bus SRAM, 8k x 8-bit through-hole DIPs are pretty cheap @ $.70. See the 3K640-I/P:
http://www.futurlec.com/ICRAM.shtml
----------------------------------------------------
Not as fast as on-board SRAM but way cheaper than a new board.
The 23K256-I/P 32k x 8-bit SPI-bus SRAM are surface mount, for $1.35 they are 4x as much.

Of course either way there's a shipping charge so save up and buy enough to justify that.

And my non-disclaimer is that I have no connection to either of those companies except being a satisfied customer. PJRC (the Teensy site) shipping got to me a lot quicker but then across the US isn't as far from me than Hong Kong. But the HK shipment 'only' took between 2-3 weeks and they DO have so many different things.

There are many other good sites but those jump out at me when it comes to 'Mo RAM!'. Hey... Moe RAM, that'd be a good forum name, wouldn't it? People could say "Hey Moe!".

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

CrossRoads

'1284's have 16K SRAM. '2560 only has 8K.
$5 processor vs $13 too.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up