I wrote a program for an Arduino DUE to control an AC-Motor over a Converter. Everything works fine until I turn OFF the installed switch and turn it ON again (Arduino is not reset). After I have done that, a false signal is output. Why is that?
Do I have to reset the Arduino whenever I turn the switch OFF? Or is there a better way to solve this problem. I think the problem might be in the if-loop.
Your help is much appreciated.
Code:
#include <SPI.h> //OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <PID_v1.h>
#define OLED_MOSI 9 // D1
#define OLED_CLK 10 // D0
#define OLED_DC 11 // DC
#define OLED_CS 12 // CS
#define OLED_RESET 13 // RES
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int analogReadPin = A0;
const int ButtonPin = A7;
const int RelaisePin = 7;
const int DACPin = DAC0;
int Button;
float faktor_tacho = 46.50;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp=0.000755, Ki=0.00755, Kd=0;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
//Filter
#include "Filter.h"
ExponentialFilter<float> volt_f(1,0);
void setup() {
Serial.begin(9600);
pinMode(analogReadPin,INPUT);
pinMode(ButtonPin, INPUT);
pinMode(RelaisePin, OUTPUT);
pinMode(DACPin, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
analogWriteResolution(12);
analogReadResolution(12);
//PID
Input = analogRead(analogReadPin);
Setpoint = 2324.5;
myPID.SetMode(AUTOMATIC);
}
void loop() {
int Voltage = analogRead(analogReadPin);
Button = analogRead(ButtonPin);
//Filter
volt_f.Filter(Voltage);
float volt = volt_f.Current();
Serial.println(volt/faktor_tacho + 0.01);
if (Button > 0){
digitalWrite(RelaisePin,HIGH);
//PID
Input = volt;
myPID.Compute();
analogWrite(DACPin, Output * 16);
display.setTextSize(4);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print(volt/faktor_tacho + 0.01);
display.println(" Hz");
display.display();
display.clearDisplay();
}
else{
analogWrite(DACPin,0);
digitalWrite(RelaisePin,LOW);
display.setTextSize(4);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("OFF");
display.display();
display.clearDisplay();
}
}