Hi All,
I'm using Arduino Nano for this project.
I have two different programs in the loop(). But some how the program 2 will randomly activate the relay, and it will switch the contact pin about 1 sec.
If I comment out either program 1 or 2, It can solved the problem.
Appreciate if can help me solve this problem. Here is the code.
I'm using Arduino Nano for this project.
I have two different programs in the loop(). But some how the program 2 will randomly activate the relay, and it will switch the contact pin about 1 sec.
If I comment out either program 1 or 2, It can solved the problem.
Appreciate if can help me solve this problem. Here is the code.
Code:
int PulsePin = 2;
int cutoff1 = 5;
int sensor1 = 6;
int sensor2 = 8;
int led = 9; //pin 9
int i = 0;
int maxCut = 81;
int minCut = 50;
const int PotPin1 = A1;
int Pot1Position_value;
const int potPin2 = A2;
int Pot2Position_value;
long last = 0;
int total = 0;
int total_2;
int count = 0;
int nPalas = 1;
unsigned long Revolutions_per_minute = 0;
int milliseconds = 50;
void setup()
{
Serial.begin(9600);
pinMode(PotPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(6, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(cutoff1, OUTPUT);
digitalWrite(cutoff1, LOW);
pinMode(PulsePin, INPUT);
}
void loop()
{
/////// Program 1 ///////
Pot1Position_value = analogRead(PotPin1);
Pot1Position_value = map(Pot1Position_value, 0, 1023, 1500, 8500);
//Serial.println(Pot1Position_value);
total = digitalRead(PulsePin);
if (total_2 != total)
{
if (total == 1 && total_2 == 0)
count++;
total_2 = total;
}
if (millis() - last >= milliseconds)
{
Revolutions_per_minute = (count / nPalas) / 2.0 * 60000.0 / (milliseconds);
if (Revolutions_per_minute > Pot1Position_value && digitalRead(sensor1) == LOW)
digitalWrite(cutoff1, HIGH);
else
digitalWrite(cutoff1, LOW);
count = 0;
last = millis();
}
/////// Program 2 ///////
Pot2Position_value = analogRead(potPin2);
Pot2Position_value = map(Pot2Position_value, 0, 1023, 50, 80);
//Serial.println(PotPosition_value);
if (Pot2Position_value > maxCut) {
Pot2Position_value = maxCut;
}
if (Pot2Position_value < minCut) {
Pot2Position_value = minCut;
}
if (digitalRead(sensor2) == HIGH) {
i = 0;
}
if (digitalRead(sensor2) == LOW && i == 0) {
i += 1;
cut();
}
}
void cut() {
digitalWrite(cutoff1, HIGH);
digitalWrite(led, LOW);
delay(Pot2Position_value);
digitalWrite(cutoff1, LOW);
digitalWrite(led, HIGH);
}