Hello everyone, I have a problem figuring out the problem in my code.
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define CLK 2
#define DT 3
#define SW 4
Adafruit_SSD1306 display(SCREEN_WIDTH,SCREEN_HEIGHT,&Wire,OLED_RESET);
Servo servo;
int trenutniCLK;
int prosliCLK;
int buttonState;
int counter = 1000;
boolean statusPWM=false;
void setup() {
servo.attach(9);
servo.writeMicroseconds(1000);
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW,INPUT_PULLUP);
prosliCLK = digitalRead(CLK);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setTextColor(WHITE);
display.setTextSize(3);
Serial.begin(9600);
}
void loop() {
trenutniCLK = digitalRead(CLK);
if (trenutniCLK != prosliCLK){
if (digitalRead(DT) != trenutniCLK) {
counter=counter+50;
if (counter>2000)
counter=2000;
} else {
counter=counter-50;
if (counter<1000)
counter=1000;
}
display.clearDisplay();
display.setCursor(10,5);
display.print("PWM");
display.setCursor(50,35);
display.print(counter);
display.display();
}
if (Serial.available()>0) {
int pwm=Serial.parseInt();
if (pwm>=1000 && pwm<=2000) {
counter=pwm;
display.clearDisplay();
display.setCursor(10,5);
display.print("PWM");
display.setCursor(50,35);
display.print(counter);
display.display();
}
else if(pwm=0) {
counter==1000;
display.clearDisplay();
display.setCursor(10,5);
display.print("PWM");
display.setCursor(50,35);
display.print(counter);
display.display();
}
}
prosliCLK = trenutniCLK;
buttonState=digitalRead(SW);
if (buttonState==LOW && statusPWM==false) {
servo.writeMicroseconds(counter);
delay(300);
while(digitalRead(SW)==LOW);
}
if (buttonState==LOW && statusPWM==true){
servo.writeMicroseconds(1000);
delay(300);
while(digitalRead(SW)==LOW);
}
statusPWM=!statusPWM;
}
So, this is my code. Basically, this is a PWM generator, I pick the PWM signal in microseconds, using an encoder and at the same time showing the PWM on my display. I can also input PWM through serial monitor, but I can't seem to type in 0 in serial monitor and get 1000microseconds on the output. That would be the first problem.
Secondly, I can't seem to figure out the problem with the booelan data, statusPWM. Going through the loop, since the statusPWM is false, the first if condition (buttonState==LOW && statusPWM==false) should be evaluated. Second if condition isn't evaluated since statusPWM is still false. At the end of the loop statusPWM is inverted so I get statusPWM=true and going through the loop again should make the first if condition not evaluated, and the second one evaluated. This should basically mean - I pick a PWM value with the encoder, press the encoder button and get that PWM on the output, and when I press again it should go down to 1000 microseconds on the output , meaning it would turn off. The problem is it won't work as expected. It doesn't respond properly. When I press the button , it gets the PWM I wanted, but when I press it again, it doesn't go back to 1000 microseconds, but after a few more presses it does that. I don't understand how should I type the code so the encoder button responds properly everytime, so that with each encoder button press I get a different result, as expected. I put the delay(300) in order to fix the bounce issue, hopefully that's correct. I hope I gave good information, first time writing here. Thanks for the help!