Ok. ill try to make this as short as possible.
The project is basically some form of a prototype for a polyphonic synthesiser.
there are 4 switches connected to an edge triggered mono pulse generator connected to an encoder whos outputs and 'valid' output is connected to the arduino duemilanove(Atmega 328) board.
4Switches -> pulse Generator -> encoder -> Arduino
The 'valid' output of encoder is connected to the Arduino pin 2 to act as an external interrupt.
when any key is pressed or released(edge triggered), the valid, will drop low for a few millis thus triggering the external interrupt on the arduino.
the arduino will then read the value of the encoder outputs and process it.
Simultaneously, there is a timer interrupt designed to output sound samples.
All variables shared between interrupts are DECLARED VOLATILE.
There are NO LOCAL VARIABLES.
The problem is, that each time i press or release the switch about 20 to 30 bytes of ram is eaten up. The following is the code:
#include <SD.h>
Sd2Card a;
uint8_t buff[512];
volatile double notefact[4]={0,0,0,0};
volatile int8_t noteID[4] = {-1,-1,-1,-1};
volatile int32_t noteTime[4] = {0,0,0,0};
volatile uint8_t temp_byte, temp2_byte, temp_bytefinal,finalbit;
volatile double temp_dbl;
uint8_t * heapptr, * stackptr;
volatile const int sampleRate = 25000;
#define SRRatio 267904.57886781300191011839336786/sampleRate
#define TWOPOWER1_12 1.0594630943592952645618252949463
#define TWOPOWER_1_12 0.94387431268169349664191315666753
volatile int8_t note = -1;
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
temp2_byte = 0;
for(int i=0; i<4;++i)
if(noteID[i]+1)
{
temp2_byte += buff[((uint32_t)(notefact[i]*noteTime[i]+0.5)) & 0x000001FF];
noteTime[i]++;
}
temp_byte = temp2_byte<<2;
//OUTPUT TO BE CODED
}
inline uint8_t readNote()
{
//Function to Read The value from the encoder
temp_byte = !digitalRead(A2);
temp_byte <<= 1;
temp_byte |= !digitalRead(A1);
temp_byte <<= 1;
temp_byte |= !digitalRead(A0);
Serial.print("Note: "); Serial.println(temp_byte);
delay(300);
return temp_byte;
}
inline double noteFactor(uint8_t y)
{
//some calculation pertaining to frequency of the note
temp_dbl=1;
while(y>0)
{
temp2_byte *= TWOPOWER1_12;
}
return temp_dbl*SRRatio;
}
void alterNote()
{
//The next three lines of code are for checking the ram, disabling timer compare interrupts
//And enabling all other interrupts in order to allow for Serial transfer(i beleive this needs interrupts Enabled);
TIMSK1 &= ~(1<<OCIE1A);
check_mem();
sei(); //Enable all interrupts
Serial.print("RA1: ");
Serial.println((int)(stackptr - heapptr)); //Print amount of RAM
note = readNote();
if(note == 4) note = 0;
else if (note == 5) note = 4;
else if (note == 6) note = 7;
else if(note == 7) note = 11;
for(int i=0; i<4; ++i)
if(noteID[i] == note)
{
notefact[i] = 0;
noteID[i] = -1;
noteTime[i] = 0;
return;
}
for(int i=0; i<4; ++i)
if(noteID[i] == -1)
{
notefact[i] = noteFactor(note);
noteID[i] = note;
noteTime[i] = 0;
return;
}
TIMSK1 |= (1<<OCIE1A); //Enable Timer Compare Interrupts
}
void setup()
{
Serial.begin(9600);
//Initialise Serial connection and Memory Card
if(a.init(SPI_HALF_SPEED,10))
Serial.println("yay");
else
Serial.print("Oh dear!");
delay(1000);
noInterrupts();
//Attach External Interrupt
attachInterrupt(0,alterNote,FALLING);
//Load The Sound Data stored in the 512 Bytes of Sector 21 of the SD card
a.readBlock(21,buff);
//SETUP TIMER REGISTERS
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1600; // compare match register 16MHz/256/2Hz
// This is a particularly High value.
// Intended value is 80
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS11); // 8 ptemp_bytecaler
check_mem();
interrupts();
delay(300);
Serial.print("RAM: ");Serial.println((heapptr-stackptr));
delay(300);
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt*/
}
//Sets stackptr and heapptr in order to calculate memory
void check_mem() {
stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
heapptr = stackptr; // save value of heap pointer
free(stackptr); // free up the memory again (sets stackptr to 0)
stackptr = (uint8_t *)(SP); // save value of stack pointer
}
void loop()
{
}
Moderator edit: Mind your language
Any Explanation would be helpful
Thanks in Advance