简体   繁体   中英

Creating a wave using MEGA2560 (analogWrite)

Been trying to create a wave using the PWM ports (because this Arduino doesn't have DAC)of an Arduino Mega using this code. In the simulation I use a wave form generator that goes to A0, then I just want to convert it from 1023 bits to 255 but I get nothing as output.

int in = A0;
int out = 10;

void setup()
{
  pinMode(in, INPUT);
  pinMode(out, OUTPUT);
}

void loop(){
  analogRead(in);
  analogWrite(10, in/4);
}

Any suggestion would be great, thanks in advance!

You're discarding the returned value from analogRead . Change:

void loop(){
  analogRead(in);
  analogWrite(10, in/4);
}

to:

void loop(){
  int p = analogRead(in);
  analogWrite(out, p / 4);
}

The pin 10 is digital output, isn't it ?

Moreover there is a function for creating a wave : tone(pin, freq, time);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM