Go Down

Topic: Trouble generating a sine wave and determining cycles per second (HZ) (Read 1 time) previous topic - next topic

sam1492

I am studying psychology at university and this is for my experiment. All I need to do is generate a sine wave flickering stimulus that is present for one second. I need a way of accurately determining the refresh rate and knowing how to properly calculate this. Using the code below, I can make a pulsating light. I need a 50% duty cycle, so it is on and off half the time. Apparently using delay(1) is not a proper way to do this. Does any body else have any suggestions?  Below is my attempt at making the LED flash at a rate of 10Hz. Thank you.


int LED =5;

int py_byte;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Baud
  pinMode(12, OUTPUT);

}

void loop() {
  if(Serial.available() >0){
    py_byte = Serial.read();
                                            // use what data is in the book for delays, then take 1000, divide it by delay for repetitions, so that it lasts 1 second
/////
{

  float in, out;

  for(int i = 0; i<10; i = i + 1){
  for (in = 0; in < 6.283; in = in + 0.006283)   // was in + in < 6.283
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LED,out);
    delay(1);
  }
}}

Go Up