سؤال

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);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top