adjustable pwm

Hello,
could someone help me how to join this two sketches together to get pwm with adjustable parameters (frequency, duty…):?
And where to put this

void instructions()

{
Serial.println("Send values followed by one of the following tags:");
Serial.println(" p - sets period in microseconds");
Serial.println(" f - sets frequency in Hz");
Serial.println(" w - sets pulse width in microseconds");
Serial.println(" c - sets duty cycle in %");
Serial.println("\n(duty cycle can have one decimal place)\n");
}

to get serial monitor print it?

Thanks

  • 1 sketch:
#include <TimerOne.h>
const char SET_PERIOD_HEADER = 'p';
const char SET_FREQUENCY_HEADER = 'f';
const char SET_PULSE_WIDTH_HEADER = 'w';
const char SET_DUTY_CYCLE_HEADER = 'c';
#define pwmRegister OCR1A // the logical pin, can be set to OCR1B
const int outPin = 9; // the physical pin
614 | Chapter 18: Using the Controller Chip Hardware
long period = 1000; // the period in microseconds
int duty = 512; // duty as a range from 0 to 1024, 512 is 50% duty cycle
int prescale[] = {0,1,8,64,256,1024}; // the range of prescale values
void setup()
{
Serial.begin(9600);
pinMode(outPin, OUTPUT);
Timer1.initialize(period); // initialize timer1, 1000 microseconds
Timer1.pwm(9, duty); // setup pwm on pin 9, 50% duty cycle
}
void loop()
{
processSerial();
}
void processSerial()
{
static long val = 0;
if ( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is ch a number?
{
val = val * 10 + ch - '0'; // yes, accumulate the value
}
else if(ch == SET_PERIOD_HEADER)
{
period = val;
Serial.print("Setting period to ");
Serial.println(period);
Timer1.setPeriod(period);
Timer1.setPwmDuty(outPin, duty); // don't change the duty cycle
show();
val = 0;
}
else if(ch == SET_FREQUENCY_HEADER)
{
if(val > 0)
{
Serial.print("Setting frequency to ");
Serial.println(val);
period = 1000000 / val;
Timer1.setPeriod(period);
Timer1.setPwmDuty(outPin, duty); // don't change the duty cycle
}
show();
val = 0;
}
else if(ch == SET_PULSE_WIDTH_HEADER)
18.5 Creating a Pulse Generator | 615
{
if( setPulseWidth(val) ) {
Serial.print("Setting Pulse width to ");
Serial.println(val);
}
else
Serial.println("Pulse width too long for current period");
show();
val = 0;
}
else if(ch == SET_DUTY_CYCLE_HEADER)
{
if( val >0 && val < 100)
{
Serial.print("Setting Duty Cycle to ");
Serial.println(val);
duty = map(val,1,99, 1, ICR1);
pwmRegister = duty;
show();
}
val = 0;
}
}
}
bool setPulseWidth(long microseconds)
{
bool ret = false;
int prescaleValue = prescale[Timer1.clockSelectBits];
// calculate time per tick in ns
long precision = (F_CPU / 128000) * prescaleValue ;
period = precision * ICR1 / 1000; // period in microseconds
if( microseconds < period)
{
duty = map(microseconds, 0,period, 0,1024);
if( duty < 1)
duty = 1;
if(microseconds > 0 && duty < RESOLUTION)
{
Timer1.pwm(outPin, duty);
ret = true;
}
}
return ret;
}
void show()
{
Serial.print("The period is ");
Serial.println(period);
Serial.print("Duty cycle is ");
// pwmRegister is ICR1A or ICR1B
616 | Chapter 18: Using the Controller Chip Hardware
Serial.print( map( pwmRegister, 0,ICR1, 1,99));
Serial.println("%");
Serial.println();
}

and 2. sketch:

int potPin = A0;
int motorPin = 9;
int potValue = 0;
int motorValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin);  
motorValue = map(potValue, 1023, 0, 0, 255);
analogWrite(motorPin, motorValue);  
Serial.print("potentiometer = " );     
Serial.print(potValue);
Serial.print("\t svjetlo = ");
Serial.println(motorValue);
delay(2);    
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Here is a sketch that is the same as what you want to do. I deleted my code and just left the structure. You will find that the switch-case is much easier to understand than all the if's you are using. Hope this helps.

/*Timers_MB.ino for the multi-board timer library
*/


void setup() {
  Serial.begin(9600);
  delay(5000);            //5 seconds to get serial monitor started
  Serial.println("You can issue commands like:");
  Serial.println("  12345p - set value to 12345 microseconds");
  Serial.println("  p - get value in microseconds");
  Serial.println("  o - turn on a simple counter as the overflow function");
  Serial.println("  n - turn off the overflow function");
  Serial.println("  b - print the counter from the oveflow function");
  Serial.println("  r - reports actual value/freq");
  Serial.println("  d - disables the timer");
  Serial.println("  e - enables the timer");
  Serial.println();
}

void loop() {
  static TIMER_TYPE value = 0;
  if ( Serial.available()) {
    char ch = Serial.read();
    switch(ch) {
      case '0'...'9':
        value = value * 10 + ch - '0';
        break;
      case 'p':
        value = 0;
        break;
      case 'r':
        value = 0;
      case 'e':

        break;
      case 'd':

        break;
      case 'o':

        break;
      case 'n':

        break;
    }
  }
}