Sinewave with 360 steps wavetable , using the PROGMEM, as configured in the
pgmspace.h library, variable in time by the analog in of a pot, just as
the example before.
/* Includes */
#include <Wire.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
/* Wavetables */
#include "sin360.h"
/* setup() */
void setup()
{
int potRead = 0; //potentiometer read in pin Analog 0
long potValue ; //value for potentiometer read
pinMode (potRead, INPUT);
/* Enable Internal pull-up resistors through software (not required with a single module on the I2C bus) */
pinMode(A4, OUTPUT);
digitalWrite(A4, HIGH);
pinMode(A5, OUTPUT);
digitalWrite(A5, HIGH);
/* Init bus I2C */
Wire.begin();
}
/* loop() */
void loop(){
/* Variables */
static uint16_t sample, i;
/* Look-up table (aka wavetable) */
for(i = 0; i < 360; ++i) {
/* Read a sample of the wavetable */
sample = pgm_read_word(sin360 + i);
int potRead = 0; //potentiometer read in pin Analog 0
long potValue ; //value for potentiometer read
potValue = analogRead(potRead);
//we map amalog 0-1023/1-4096 for DAC:
potValue = map(analogRead(potRead), 0, 1023, 1, 4096);
/* Starts communication with the MCP4725 */
Wire.beginTransmission(B1100000);
/* Sending the command "DAC Update Register" */
Wire.write(B1000000);
/* Sending the 12-bit data */
Wire.write((sample & 0xFF0) >> 4); // The 8 bits in first
Wire.write((sample & 0xF) << 4); // Then the 4 bits of Least Significant Bit- LSB
_delay_us(potValue); // delay 30 ms, acording to <util/delay.h
/* Stop commnication MCP4725 */
Wire.endTransmission();
}
}
"sin360.h"
uint16_t sin360[] PROGMEM = {
0, 1, 2, 4, 7, 11, 15, 19, 25, 31, 37, 44, 52, 60, 69, 79,
89, 100, 111, 123, 135, 149, 162, 176, 191, 207, 223,
239, 256, 274, 292, 311, 330, 349, 370, 390, 412, 433, 456,
478, 502, 525, 549, 574, 599, 625, 650, 677, 704, 731, 758,
786, 815, 843, 872, 902, 932, 962, 992, 1023, 1054, 1085,
1117, 1149, 1181, 1214, 1247, 1280, 1313, 1346, 1380, 1414,
1448, 1482, 1517, 1551, 1586, 1621, 1656, 1691, 1726, 1762,
1797, 1833, 1868, 1904, 1939, 1975, 2011, 2046, 2047, 2082,
2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780,
2813, 2846, 2879, 2912, 2944, 2976, 3008, 3039,
3070, 3101, 3131, 3161, 3191, 3221, 3250, 3278, 3307, 3335,
3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544, 3568, 3591,
3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958,
3970, 3982, 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056,
4062, 4068, 4074, 4078, 4082, 4086, 4089, 4091, 4092, 4093,
4094, 4093, 4092, 4091, 4089, 4086, 4082, 4078, 4074, 4068,
4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004, 3993, 3982,
3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637,
3615, 3591, 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389,
3362, 3335, 3307, 3278, 3250, 3221, 3191, 3161, 3131, 3101,
3070, 3039, 3008, 2976, 2944, 2912, 2879, 2846, 2813, 2780,
2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507, 2472, 2437,
2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
2047, 2011, 1975, 1939, 1904, 1868, 1833, 1797, 1762, 1726,
1691, 1656, 1621, 1586, 1551, 1517, 1482, 1448, 1414, 1380,
1346, 1313, 1280, 1247, 1214, 1181, 1149, 1117, 1085, 1054,
1023, 992, 962, 932, 902, 872, 843, 815, 786, 758, 731, 704,
677, 650, 625, 599, 574, 549, 525, 502, 478, 456, 433, 412,
390, 370, 349, 330, 311, 292, 274, 256, 239, 223, 207, 191,
176, 162, 149, 135, 123, 111, 100, 89, 79, 69, 60, 52, 44,
37, 31, 25, 19, 15, 11, 7, 4, 2, 1, 0
};