// PWM-test-2// Translate potmeter position into frequence and duty cycle of PWM signal/*-----( Import needed libraries )-----*/#include <Wire.h> // Comes with Arduino IDE#include <LCD.h>// Get the LCD I2C Library here: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads#include <LiquidCrystal_I2C.h>// set the LCD address to 0x27 for a 16 chars 2 line display// Set the pins on the I2C chip used for LCD connections:// addr, en,rw,rs,d4,d5,d6,d7,bl,blpolLiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address/*-----( Define constants and variables )------*/unsigned long pwm1; // Value read from A03 to give PWM duty cycle output in terms // of 0-5Vunsigned long potMeter;const byte pinPotmeter = 3;const byte pinMotor1 = 4;const byte pinMotor2 = 5;const byte pinTimer2a = 3;const byte pinTimer2b = 11;const byte MaxStep = 64;int potmeterLimits[] = {10, 26, 42, 57, 73, 89, 105, 121, 137, 152, 168, 184, 200, 216, 232, 247, 263, 279, 295, 311, 327, 342, 358, 374, 390, 406, 422, 437, 453, 469, 485, 501, 517, 532, 548, 564, 580, 596, 611, 627, 643, 659, 675, 691, 706, 722, 738, 754, 770, 786, 801, 817, 833, 849, 865, 881, 896, 912, 928, 944, 960, 976, 991, 1007, 1023};// For display purpose only, required values for PWM Frequency and Duty Cycle.int Freq_Hashing[] = {31, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 96, 97, 98, 99, 100};int DC_Hashing[] = {0, 1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 38, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 50, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 63, 64, 66, 67, 68, 69, 70, 71, 73, 74, 75};// Values for OCnA and OCnB, calculated from PWM Frequency and Duty Cycle.int OCnA_Hashing[] = {255, 255, 247, 238, 231, 223, 217, 210, 204, 199, 193, 188, 183, 179, 174, 170, 166, 162, 159, 155, 152, 149, 146, 143, 140, 137, 135, 132, 130, 128, 125, 123, 121, 119, 117, 115, 113, 112, 110, 108, 107, 105, 104, 102, 101, 99, 98, 97, 95, 94, 93, 92, 91, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 79};int OCnB_Hashing[] = {0, 2, 4, 7, 9, 12, 14, 16, 18, 20, 21, 23, 24, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 41, 42, 43, 44, 44, 45, 46, 46, 47, 47, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 54, 55, 55, 56, 56, 56, 57, 57, 57, 57, 58, 59};void setup(){ // Initialize motor pins pinMode(pinMotor1, OUTPUT); pinMode(pinMotor2, OUTPUT); // Set motor pins unequal to ensure movement controlled by enable digitalWrite(pinMotor1, HIGH); digitalWrite(pinMotor2, LOW); // Set timer 2 op phase-correct PWM with OCRA controlling top limit, prescaler 1024 pinMode(pinTimer2a, OUTPUT); pinMode(pinTimer2b, OUTPUT); TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20); TCCR2B = _BV(WGM22) | _BV(CS22) | _BV(CS21) | _BV(CS20); // Prescaler Value is 1024 // Initialize lcd screen lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines lcd.backlight(); // Turn backlight on}byte getPotmeterSetting(){ byte iCheck = 0; // Read potmeter value; potMeter = analogRead(pinPotmeter); // Determine matching index value and return it while ((potMeter > potmeterLimits[iCheck]) && (iCheck < MaxStep)) { iCheck++; } return iCheck;}void setTimer2(int iIndex){ //Assign values to OCR Registers, which determine PWM frequency and duty cycle. OCR2A = OCnA_Hashing[iIndex]; OCR2B = OCnB_Hashing[iIndex]; // Display various variables on LCD screen lcd.clear(); lcd.setCursor(0, 0); lcd.print(potMeter); lcd.print("["); lcd.print(iIndex); lcd.print("] "); lcd.print(OCnA_Hashing[iIndex]); lcd.print("/"); lcd.print(OCnB_Hashing[iIndex]); lcd.setCursor(0, 1); lcd.print(Freq_Hashing[iIndex]); lcd.print("Hz, "); lcd.print(DC_Hashing[iIndex]); lcd.print("%");}void loop(){ // Check potmeter value and set frequency and duty cycle accordingly setTimer2(getPotmeterSetting()); // Wait half a second delay(500);}