Hey all, I have a code that allows me to generate a 25 to 65khz signal via a potentiometer using the fast PWM registers and it works perfectly. I just cant get my head around how I can set this frequency for between 200 and 400khz. Can anyone point me in the right direction please?
const int pot = 0;
const int clk = 6;
//Variable will change
int potVal = 0; // pot pin value
int lastPotVal = 0; // previous state of pot pin
void setup() {
pinMode(pot, INPUT);
pinMode(clk, OUTPUT);
// wgm 14, prescaler 1, output pin OC1A
TCCR1A = _BV(COM1A1) | _BV(WGM11);
TCCR1B = _BV(CS10) | _BV(WGM12) | _BV(WGM13);
}
void loop() {
//first read in warp pot value
potVal = analogRead(pot);
int potValmapped;
ICR1 = potValmapped ; // sets frequency (TOP limit, the number of prescaled clock cycles
// before the timer counter is reset and OC1A is set
OCR1A = potValmapped / 2 ; // 50% duty cycle. The point at which OC1A is cleared.
//change the value if the pot moved
if (potVal != lastPotVal){
// read sensor value from warp read pin
int potVal = analogRead(pot);
// map sensor value to range for pwm freq
int potValmapped = map(potVal, 1, 1023, 25000, 80000);
// save current value for next time through the loop
lastPotVal = potVal;
// output that value to clk pin for FV-1
tone(clk, potValmapped);
}
// Delay a little bit to avoid bouncing
delay(5);
// save the current state as the last state,
// for next time through the loop
lastPotVal = potVal;
}
Please use Code Tags </> to present code. As is your code does not make much sense
Hi DrDiettrich, sorry about that, I'm very new to the arduino hardware and programming. I've been given this task from my boss looking for a solution for a customer. Any assistance would be greatly appreciated.
@greggytibbs you posted in the wrong section. That section is not for questions about your project. Please be more careful when you choose where to post in future.
Your understanding of the C/C++ programming language looks not very promising. Try to get assistance in Local Groups or Jobs and Paid Consultancy.
You are using too many duplicate variables. Also choose tone() or low level timer code.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.