/*This was from: https://circuitdigest.com/microcontroller-projects/arduino-waveform-gen…
*
* The sine wave part didn't work for me and made project flakey. I first changed this to I2C LCD then later
* to the .96" ssd1306 OLED so it would be a small project box unit. Also I switch from an Arduino Nano to a
* bare bones Arduino Uno. Took a lot of searching to find the thread to clear part of a line in the OLED.
* Built this into a small project case.
*/
#include <PWM.h> //PWM librarey for controlling freq. of PWM signal
#include <Wire.h>
#include <Adafruit_SSD1306.h> //I2C library for the OLED
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET); //create instance of the SSD1306 called display
int Encoder_OuputA = 11;
int Encoder_OuputB = 12;
int Encoder_Switch = 10;
int Previous_Output;
int multiplier = 1;
int y;
int x;
double angle = 0;
double increment = 0.2;
const int signal_pin = 9;
int32_t frequency; //frequency to be set
int32_t lower_level_freq = 1; //Lowest possible freq value is 1Hz
int32_t upper_level_freq = 100000; //Maximum possible freq is 100KHz
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //begin the OLED @ hex addy )x3C
display.display(); //show the buffer
delay(2000); //bask in the glory of LadyAda
display.clearDisplay();
display.setTextSize(1); //smallest text size
display.setTextColor(WHITE);
display.print("Signal Generator"); //Intro Message line 1
display.setCursor(0, 32);
display.print("Circuit-Digest "); //Intro Message line 2
display.display();
delay(2000);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Freq: 0000000 Hz");
display.setCursor(0, 32);
display.print("Inc. by: 1 ");
display.display();
InitTimersSafe(); //Initialize timers without disturbing timer 0
/*This was from: https://circuitdigest.com/microcontroller-projects/arduino-waveform-gen…
*
* The sine wave part didn't work for me and made project flakey. I first changed this to I2C LCD then later
* to the .96" ssd1306 OLED so it would be a small project box unit. Also I switch from an Arduino Nano to a
* bare bones Arduino Uno. Took a lot of searching to find the thread to clear part of a line in the OLED.
* Built this into a small project case.
*/
#include <PWM.h> //PWM librarey for controlling freq. of PWM signal
#include <Wire.h>
#include <Adafruit_SSD1306.h> //I2C library for the OLED
#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET); //create instance of the SSD1306 called display
int Encoder_OuputA = 11;
int Encoder_OuputB = 12;
int Encoder_Switch = 10;
int Previous_Output;
int multiplier = 1;
int y;
int x;
double angle = 0;
double increment = 0.2;
const int signal_pin = 9;
int32_t frequency; //frequency to be set
int32_t lower_level_freq = 1; //Lowest possible freq value is 1Hz
int32_t upper_level_freq = 100000; //Maximum possible freq is 100KHz
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //begin the OLED @ hex addy )x3C
display.display(); //show the buffer
delay(2000); //bask in the glory of LadyAda
display.clearDisplay();
display.setTextSize(1); //smallest text size
display.setTextColor(WHITE);
display.print("Signal Generator"); //Intro Message line 1
display.setCursor(0, 32);
display.print("Circuit-Digest "); //Intro Message line 2
display.display();
delay(2000);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Freq: 0000000 Hz");
display.setCursor(0, 32);
display.print("Inc. by: 1 ");
display.display();
InitTimersSafe(); //Initialize timers without disturbing timer 0
//pin Mode declaration
pinMode (Encoder_OuputA, INPUT);
pinMode (Encoder_OuputB, INPUT);
pinMode (Encoder_Switch, INPUT);
Previous_Output = digitalRead(Encoder_OuputA); //Read the inital value of Output A
//attachInterrupt(0,generate_sine,CHANGE); // Eliminate sine generator?
}
void loop()
{
if (digitalRead(Encoder_OuputA) != Previous_Output)
{
if (digitalRead(Encoder_OuputB) != Previous_Output)
{
frequency = frequency + multiplier;
Serial.println(frequency);
pwmWriteHR(signal_pin, 32768); //Set duty cycle to 50% by default -> for 16-bit 65536/2 = 32768
SetPinFrequencySafe(signal_pin, frequency);
for (y=0; y<=8; y++)
{
for (x=30; x<82; x++)
{
display.drawPixel(x, y, BLACK);
}
}
display.display();
display.setTextSize(1); //smallest text size
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Freq: Hz");
display.setCursor(37, 0);
display.print(frequency);
display.display();
}
else
{
frequency = frequency - multiplier;
// Serial.println(frequency);
pwmWriteHR(signal_pin, 32768); //Set duty cycle to 50% by default -> for 16-bit 65536/2 = 32768
SetPinFrequencySafe(signal_pin, frequency);
for (y=0; y<=9; y++)
{
for (x=30; x<82; x++)
{
display.drawPixel(x, y, BLACK);
}
}
display.display();
display.setTextSize(1); //smallest text size
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Freq: Hz");
display.setCursor(37, 0);
display.print(frequency);
display.display();
}
}
if (digitalRead(Encoder_Switch) == 0)
{
multiplier = multiplier * 10;
if (multiplier>1000)
multiplier=1;
for (y=32; y<=39; y++)
{
for (x=48; x<127; x++)
{
display.drawPixel(x, y, BLACK);
}
}
display.display();
display.setCursor(0, 32);
display.setTextColor(WHITE);
display.print("Inc. by ");
display.setCursor(48, 32);
display.print(multiplier);
display.display();
delay(300); // was 500
while(digitalRead(Encoder_Switch) == 0);
}
Previous_Output = digitalRead(Encoder_OuputA);
}