The code bellow is used to control a motorized potentiometer ( a 12 v dc motor) through a L298N H-bridge module.
The idea is that when i keep pressing the + or - button, the dc motor will work constantly. - that works
And when i press for a short time one of the buttons, i want the motor to work for a short amount of time (lets say 0,5 seconds). - here i keep going crazy, can't think of a way to program it so it would work.
It also has a input switch(er), that also works fine.
I ask for your help, maybe a much cleared head can think of a better approach, or a solution.
Yes, if have googled the ... out of this subject, haven't managed to find something useful.
#include <IRremote.h>
//pin receptor IR
const int RECV_PIN = 6;
//Pinii pt driver motor
const int enA = 9;
const int in1 = 7;
const int in2 = 8;
//pin buton comutare sursa audio
const int button = 5;
//pini sursa audio
const int out1 = 2;
const int out2 = 3;
const int out3 = 4;
//pin activare tranzistor
const int tranzistor = 10;
//variabile
int output;
int contor = 0;
int k=0;
unsigned long interval_pulsatie;
unsigned long last_pulsatie;
int buttonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
unsigned long lastCode;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(tranzistor, OUTPUT);
//activam IR receiver
irrecv.enableIRIn();
//activare tranzistor si intarziere 12 sec
//delay(15000);
digitalWrite(tranzistor, HIGH);
//activam output 1 cu intarziere 1 sec
//delay(1000);
digitalWrite(out1, HIGH);
output = 1;
analogWrite(enA, 50);
}
void loop() {
if(irrecv.decode(&results)){ // verifica receptionare cod
Serial.println(results.value, HEX);
if (results.value == 0xFFE01F || results.value == 0xF076C13B ||
results.value == 0xFFA857 || results.value == 0xA3C8EDDB ||
results.value == 0xFFFFFFFF){
if(results.value == 0xFFFFFFFF){
if (lastCode == 0xFFE01F || lastCode == 0xF076C13B ||
lastCode == 0xFFA857 || lastCode == 0xA3C8EDDB){
// daca se repeta, folosim ultimul cod primit
results.value = lastCode;
}
}
// buton volum -
if(results.value == 0xFFE01F || results.value == 0xF076C13B){
last_pulsatie = interval_pulsatie;
interval_pulsatie = millis();
//Serial.print(interval_pulsatie - last_pulsatie);
lastCode = results.value;
digitalWrite(in2, LOW);
digitalWrite(in1, HIGH);
contor = 1;
}
//buton volum +
if(results.value == 0xFFA857 || results.value == 0xA3C8EDDB) {
last_pulsatie = interval_pulsatie;
interval_pulsatie = millis();
//Serial.print(interval_pulsatie - last_pulsatie);
lastCode = results.value;
digitalWrite(in2, HIGH);
digitalWrite(in1, LOW);
contor = 1;
}
}
// button sursa
if(results.value == 0xFF906F){
if(output == 1 && k == 0){
digitalWrite(out1, LOW);
delay(20);
digitalWrite(out2, HIGH);
output = 2;
delay(50);
results.value = 0;
k = 1;
}
if(output == 2 && k == 0){
digitalWrite(out2, LOW);
delay(20);
digitalWrite(out3, HIGH);
output = 3;
delay(500);
results.value = 0;
k = 1;
}
if(output == 3 && k == 0){
digitalWrite(out3, LOW);
delay(20);
digitalWrite(out1, HIGH);
output = 1;
delay(500);
results.value = 0;
k = 1;
}
k = 0;
}
if (!(results.value == 0xFFE01F || results.value == 0xF076C13B ||
results.value == 0xFFA857 || results.value == 0xA3C8EDDB ||
results.value == 0xFFFFFFFF || results.value == 0xFF906F )){
lastCode = 0;
Serial.println("0");
}
irrecv.resume();
}
else{
if (contor == 1 && millis() - last_pulsatie > 250){
//Serial.println(" intrerupe");
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
contor = 0;
}
// else{
//delay(1000);
//digitalWrite(in1, LOW);
//digitalWrite(in2, LOW);
//}
}
/*
if(digitalRead(button) == LOW){
if(output == 1){
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH);
output = 2;
delay(1000);
}
}
if(digitalRead(button) == LOW){
if(output == 2){
digitalWrite(out2, LOW);
digitalWrite(out3, HIGH);
output = 3;
delay(1000);
}
}
if(digitalRead(button) == LOW){
Serial.println("error");
if(output == 3){
digitalWrite(out3, LOW);
digitalWrite(out1, HIGH);
output = 1;
delay(1000);
}
}
*/
}