In the following code, can I change ' ICR1 = 320; ' as ' ICR1 = val; ' the 'val' from ' val = analogRead(potPin); '?
Thanks
// PWM output @ 25 kHz, only on pins 9 and 10.
// Output value should be between 0 and 320, inclusive.
void analogWrite25k(int pin, int value)
{
switch (pin) {
case 9:
OCR1A = value;
break;
case 10:
OCR1B = value;
break;
default:
// no other pin will work
break;
}
}
void setup()
{
// Configure Timer 1 for PWM @ 25 kHz.
TCCR1A = 0; // undo the configuration done by...
TCCR1B = 0; // ...the Arduino core library
TCNT1 = 0; // reset timer
TCCR1A = _BV(COM1A1) // non-inverted PWM on ch. A
| _BV(COM1B1) // same on ch; B
| _BV(WGM11); // mode 10: ph. correct PWM, TOP = ICR1
TCCR1B = _BV(WGM13) // ditto
| _BV(CS10); // prescaler = 1
ICR1 = 320; // TOP = 320
// Set the PWM pins as output.
pinMode( 9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
// Just an example:
analogWrite25k( 9, 110);
analogWrite25k(10, 210);
for (;;) ; // infinite loop
}
What happened when you tried?
AWOL:
What happened when you tried?
when I changed the ' ICR1 = 320; ' as ' ICR1 = val; ' the 'val' , and added some thing, there is no output at pin9 and pin10.
// PWM output @ 25 kHz, only on pins 9 and 10.
// Output value should be between 0 and 320, inclusive.
int potPin = A2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int potval = 0;
int value = 0;
int valread=0;
void setup()
{
// Configure Timer 1 for PWM @ 25 kHz.
TCCR1A = 0; // undo the configuration done by...
TCCR1B = 0; // ...the Arduino core library
TCNT1 = 0; // reset timer
TCCR1A = _BV(COM1A1) // non-inverted PWM on ch. A
| _BV(COM1B1) // same on ch; B
| _BV(WGM11); // mode 10: ph. correct PWM, TOP = ICR1
TCCR1B = _BV(WGM13) // ditto
| _BV(CS10); // prescaler = 1
//ICR1 = 320; // TOP = 320
ICR1 = valread; // TOP = 320
// Set the PWM pins as output.
Serial.begin(9600); // Debugging only
Serial.println("setup"); //Prints "Setup" to the serial monitor
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
val = analogRead(potPin); // read the value from the sensor
Serial.println("READ"); //Prints "Setup" to the serial monitor
Serial.println(val);
val = map (val,0,1023,0,320);
valread =map(val,0,1023,0,320);
Serial.println("READ"); //Prints "Setup" to the serial monitor
Serial.println(val);
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val);
analogWrite(9, val);
analogWrite(10, 50);
}
ICR1 = valread; // TOP = 320
That's a rather dumb name for a variable when it does not contain (at least, not yet) a value read from anything.
You seem to be operating under the mistaken assumption that this binds the value of ICR1 to the value in the variable. It does NOT. It makes a one time assignment. If you always want the value in ICR1 to be the same as the value in valread, then every time the value in valread changes, you need to change the value in ICR1.
Which means that the valread variable is useless.