Contents

Sine wave code

/*
 * Sine wave : plain code
 * A. J. Misquitta based on code by  Amanda Ghassaei
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
*/

byte sine[] = {127, 134, 142, 150, 158, 166, 173,
181, 188, 195, 201, 207, 213, 219, 224, 229, 234,
238, 241, 245, 247, 250, 251, 252, 253, 254, 253,
252, 251, 250, 247, 245, 241, 238, 234, 229, 224,
219, 213, 207, 201, 195, 188, 181, 173, 166, 158,
150, 142, 134, 127, 119, 111, 103, 95, 87, 80, 72,
65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6,
3, 2, 1, 0, 0, 0, 1, 2, 3, 6, 8, 12, 15, 19, 24,
29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119,};

int samples = 100; // These are the number of samples in sine[]

void setup(){
  //set digital pins 0-7 as outputs
  for (int i=0;i<8;i++){
    pinMode(i,OUTPUT);
  }
}

void loop(){
  for (int t=0; t < samples; t++){
    PORTD = sine[t];  // Use this for fast sampling
    //PORTD = 127+127*sin(2*3.1415926*t/100); // and this for slow sampling 
    
    /* The period of the wave can be controlled using the delay. 
     * But remember that other functions in the loop also effect
     * the period!
     */
    delayMicroseconds(10);  //wait 10us
  }
}

AJMPublic/teaching/arduino-pi/projects/arduino/dac-audio/code-sine (last edited 2021-04-14 13:14:40 by apw109)