Sometimes you want to check analog signals and there is no scope around? This is all you need to code:
Just connect your signal to the A0 pin (and GND of course). Make sure the signal is in the well-known limits.
Actually, as you can see it is not really true. There is a recursive macro included in file "tab.h", it looks like this:
#define N 198
analogRead(A0),
analogRead(A0),
analogRead(A0),
#if __COUNTER__< N
#include __FILE__
#endif
Doing it this way the analogRead is called 198 * 3 = 594 times (more than enough to fill the Serial plotter) during the phase when the array gets initialized, each time the loop is called. It is done as fast as the processor can, no repeat instructions necessary. (198 is the maximum depth for recursions.)
Part 2: analogWriteThe same trick can be applied to calculating values at compile-time and storing them in an array in FLASH (ROM) and reading array values from there at runtime: (read the comments for detailed information)
/*
The include file includes itself.
For this nesting there is a limit of 198
(maximum depth of recursion).
As it contains __COUNTER__ twice you
can set the limit to 2 * 198 = 396.
----------------------------------------
In case you want to get more values
that can be done but it takes
significantly more steps.
*/
#define M 256.0
#define N 396
const PROGMEM byte tab[] = {
/*
you can select sine or sawtooth
(other waveforms are possible)
*/
#include "sin.h"
//#include "saege.h"
};
/*
Now the whole table is stored in FLASH.
Only 188 bytes of dynamic storage (RAM)
are used, mainli for the Serial.println
function.
*/
const byte SPK = 10;
void setup() {
Serial.begin(9600);
for (int i = 0; i < sizeof tab; i++)
Serial.println(pgm_read_byte(&(tab[i])));
TCCR1B = 1; // default value: 3
}
void loop() {
for (int i = 0; i < sizeof tab; i++) {
/*
As the values have to be read from the
ROM rather from RAM you need to insert
pgm_read_byte(& before the variable name.
Unfortunately accessing values from ROM
takes one more processor cycle each time.
*/
analogWrite(SPK, pgm_read_byte(&(tab[i])));
/*
delay whatever you want
*/
delayMicroseconds(10);
}
}
The include files look like this:
saege.h
__COUNTER__ * M / N,
#if __COUNTER__ < N
#include __FILE__
#endif
sin.h
(byte) ((sin(__COUNTER__ * TWO_PI /N) + 1) * 127),
#if __COUNTER__ < N
#include __FILE__
#endif
Comments
Please log in or sign up to comment.