Hi. i am new to coding and i am trying to make square wave generator(50% duty cycle)but i ran into a problem. The code that i found and use starts jumping numbers when the frequency gets higher(and i want it to go up or down one at a time).I tried to modify the code but with no success.
Can you please help me?...Thank you and have a nice day!
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <FrequencyTimer2.h>
#include <Bounce2.h>
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3
#define BUTTON_PIN_3 4
#define LED_PIN 13
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
static const unsigned char PROGMEM logo[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFC, 0x3F, 0x80,
0x07, 0x9E, 0x00, 0x00, 0x00, 0x03, 0xFF, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x03, 0xFC, 0x3F, 0x80,
0x07, 0x9E, 0x00, 0x00, 0x00,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x07, 0x9E, 0xE7, 0x1E, 0xF0, 0x00, 0x78, 0x73, 0xBF, 0xF3, 0x9C, 0x00, 0x03, 0x0C, 0x30, 0x00,
0x07, 0x9E, 0xE7, 0x1E, 0xF0, 0x00, 0x78, 0x73, 0xBF, 0xF3, 0x9C, 0x00, 0x03, 0x0C, 0x30, 0x00,
0x07, 0x9E, 0x7F, 0x1E, 0xF0, 0x00, 0x78, 0x7F, 0x3F, 0xE3, 0xFC, 0x00, 0x7F, 0x0F, 0xF0, 0x00,
0x07, 0x9E, 0x7E, 0x1E, 0xF0, 0x00, 0x78, 0x3F, 0x3F, 0xC1, 0xF8, 0x00, 0x7F, 0x0F, 0xF0, 0x00,
0x07, 0x9E, 0x1C, 0x1C, 0xE0, 0x00, 0x78, 0x1C, 0x3F, 0x00, 0xF0, 0x00, 0x7F, 0x0F, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#define SSD1306_LCDHEIGHT 64
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
float P=7042;
unsigned int P1;
unsigned long Hout;
unsigned long Sp;
unsigned long Sm;
unsigned long H;
unsigned long N1=1;
unsigned long N2=10;
unsigned long N3=20;
unsigned long N4=150;
unsigned long E=LOW;
unsigned long E1=0;
unsigned long Min;
unsigned long Plus;
// Instantiate a Bounce object
Bounce debouncer1 = Bounce();
// Instantiate another Bounce object
Bounce debouncer2 = Bounce();
// Instantiate another Bounce object
Bounce debouncer3 = Bounce();
void setup() {
pinMode(FREQUENCYTIMER2_PIN, OUTPUT);
// Setup the first button with an internal pull-up :
pinMode(BUTTON_PIN_1,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer1.attach(BUTTON_PIN_1);
debouncer1.interval(5); // interval in ms
// Setup the second button with an internal pull-up :
pinMode(BUTTON_PIN_2,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer2.attach(BUTTON_PIN_2);
debouncer2.interval(5); // interval in ms
// Setup the second button with an internal pull-up :
pinMode(BUTTON_PIN_3,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer3.attach(BUTTON_PIN_3);
debouncer3.interval(5); // interval in ms
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.drawBitmap(0, 0, logo, 128, 32, WHITE);
display.display();
delay(2000);
display.clearDisplay();
FrequencyTimer2::setPeriod(200);
FrequencyTimer2::enable();
}
volatile unsigned long burpCount = 0;
void Burp(void) {
burpCount++;
}
void loop() {
debouncer1.update();
debouncer2.update();
debouncer3.update();
// Get the updated value :
int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
if(P1==1){
Min = Min;
}
else {
Min = Hout - (1000000/(P1+1));
}
Plus = Hout - (1000000/(P1+1));
// Turn on the LED if either button is pressed :
if ( value2 == LOW ) {
Sp++;
if(Sp > N1){
P++;}
if(Sp > N2){
P = P + 10;}
if(Sp > N3){
P = P + 100;}
if(Sp > N4){
P = P + 1000;}
if (P>100000){
P = P+5000;
}
digitalWrite(LED_PIN, HIGH );
}
if ( value1 == LOW) {
Sp++;
if(Sp > N1){
P--;}
if(Sp > N2){
P = P - 10;}
if(Sp > N3){
P = P - 100;}
if(Sp > N4){
P = P - 1000;}
digitalWrite(LED_PIN, HIGH );
}
if ( value3 == LOW) {
E1++;
if (E1 == 1){
E = !E;
}
digitalWrite(LED_PIN, HIGH );
}
if ( value1 == HIGH && value2 == HIGH && value3 == HIGH ){
E1=0;
Sp=0;
digitalWrite(LED_PIN, LOW );
}
if (E == HIGH){
FrequencyTimer2::enable();
}
if (E == LOW){
FrequencyTimer2::disable();
}
if (P<30){
P=30;
}
if (P>1000000){
P=1000000;
}
P1 = 1000000/P;
FrequencyTimer2::setPeriod(P1);
static unsigned long v = 0;
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case '0'...'9':
v = v * 10 + ch - '0';
break;
case 'p':
FrequencyTimer2::setPeriod(v);
Serial.print("set ");
Serial.print((long)v, DEC);
Serial.print(" = ");
Serial.print((long)FrequencyTimer2::getPeriod(), DEC);
Serial.println();
v = 0;
break;
case 'r':
Serial.print("period is ");
Serial.println(FrequencyTimer2::getPeriod());
break;
case 'e':
FrequencyTimer2::enable();
break;
case 'd':
FrequencyTimer2::disable();
break;
case 'o':
FrequencyTimer2::setOnOverflow(Burp);
break;
case 'n':
FrequencyTimer2::setOnOverflow(0);
break;
case 'b':
unsigned long count;
noInterrupts(); // disable interrupts while reading the count
count = burpCount; // so we don't accidentally read it while the
interrupts(); // Burp() function is changing the value!
Serial.println(count, DEC);
break;
}
}
Hout = 1000000/FrequencyTimer2::getPeriod();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(Hout);
display.println("Hz");
if (E == LOW){
display.println("OFF");
}
if (E == HIGH){
display.println("ON");
}
//display.println(P1);
//display.println(Sp);
display.display();
}