2016-08-14 3 views
1

Ich habe ein Problem mit meinen Relais-Schaltern. Ich habe einen 5V, vier Relaisschalter für den Einsatz mit einem Arduino. Ich versuche es so zu machen, dass wenn ich einen Knopf drücke, ein Relais anspringt und dann, wenn ich es wieder drücke, das selbe Relais ausgeht.Arduino Relay Button

Dieses Konzept funktioniert mit dem Code bei Verwendung eines Relais. Das Problem ist jedoch, dass mein Code nur für ein Relais und nur für ein Relais funktioniert. Wenn ich den Code ändere und mehrere Variablen mache, wird es nicht funktionieren.

FYI, ich bin mit einem Arduino UNO R3 ATmega 328

Beachten Sie, dass dieser erste Code funktioniert, aber nur für ein Relais. Es funktioniert, wenn ich den Knopf drücke, um es einzuschalten, und dann den Knopf wieder drückend, schalte es aus.

const int rl1 = 7; 
const int rl2 = 12; 
const int rl3 = 2; 
const int rl4 = 8; 
const int button1 = 11; 
const int button2 = 10; 
const int button3 = 3; 
const int button4 = 4; 

int rl1State = LOW; 
int rl2State = LOW; 
int rl3State = LOW; 
int rl4State = LOW; 
int buttonState = LOW; 
int lastButtonState = HIGH; 
int reading; 

long lastDebounceTime=0; 
long debounceDelay = 50; 


void setup() { 

    Serial.begin(9600); 
    pinMode(rl1, OUTPUT); 
    pinMode(rl2, OUTPUT); 
    pinMode(rl3, OUTPUT); 
    pinMode(rl3, OUTPUT); 
    pinMode(button1, INPUT); 
    pinMode(button2, INPUT); 
    pinMode(button3, INPUT); 
    pinMode(button4, INPUT); 

} 

void loop() { 

    reading = digitalRead(button1); 


    if(reading != lastButtonState){ 
    lastDebounceTime = millis(); 
    lastButtonState = reading; 
    } 

    if((millis() - lastDebounceTime) > debounceDelay){ 
    if(buttonState != lastButtonState){ 
     buttonState = lastButtonState; 
     if(buttonState == HIGH){ 
     rl1State = !rl1State; 
     digitalWrite(rl1, rl1State); 


     } 
    } 
    } 



} 

habe ich versucht, diesen Code für mehrere Relais:

const int rl1 = 7; 
const int rl2 = 12; 
const int rl3 = 2; 
const int rl4 = 8; 
const int button1 = 11; 
const int button2 = 10; 
const int button3 = 3; 
const int button4 = 4; 

int rl1State = LOW; 
int rl2State = LOW; 
int rl3State = LOW; 
int rl4State = LOW; 


//States 
int buttonState1 = LOW; 
int lastButtonState1 = HIGH; 
int buttonState2 = LOW; 
int lastButtonState2 = HIGH; 
int buttonState3 = LOW; 
int lastButtonState3 = HIGH; 
int buttonState4 = LOW; 
int lastButtonState4 = HIGH; 

//Read State 

int reading1; 
int reading2; 
int reading3; 
int reading4; 

long lastDebounceTime1=0; 
long debounceDelay1 = 50; 

long lastDebounceTime2=0; 
long debounceDelay2 = 50; 

long lastDebounceTime3=0; 
long debounceDelay3= 50; 

long lastDebounceTime4=0; 
long debounceDelay4 = 50; 


void setup() { 

    Serial.begin(9600); 
    pinMode(rl1, OUTPUT); 
    pinMode(rl2, OUTPUT); 
    pinMode(rl3, OUTPUT); 
    pinMode(rl3, OUTPUT); 
    pinMode(button1, INPUT); 
    pinMode(button2, INPUT); 
    pinMode(button3, INPUT); 
    pinMode(button4, INPUT); 

} 

void loop() { 

    reading1 = digitalRead(button1); 
    reading2 = digitalRead(button2); 
    reading3 = digitalRead(button3); 
    reading4 = digitalRead(button4); 


//Relay 1 
    if(reading1 != lastButtonState1){ 
    lastDebounceTime1 = millis(); 
    lastButtonState1 = reading1; 
    } 
    if(reading1 != lastButtonState1){ 
    lastDebounceTime1 = millis(); 
    lastButtonState1 = reading1; 
    } 
    if((millis() - lastDebounceTime1) > debounceDelay1){ 
    if(buttonState1 != lastButtonState1){ 
     buttonState1 = lastButtonState1; 
     if(buttonState1 == HIGH){ 
     rl1State = !rl1State; 
     digitalWrite(rl1, rl1State); 


     } 
    } 
    } 


    //Relay 2 
    if(reading2 != lastButtonState2){ 
    lastDebounceTime2 = millis(); 
    lastButtonState2 = reading2; 
    } 

    if((millis() - lastDebounceTime2) > debounceDelay2){ 
    if(buttonState2 != lastButtonState2){ 
     buttonState2 = lastButtonState2; 
     if(buttonState2 == HIGH){ 
     rl2State = !rl2State; 
     digitalWrite(rl2, rl2State); 


     } 
    } 
    } 



} 

Ich habe auch versucht, alle Variablen für jede Taste und Relais neu zu machen, aber es funktioniert immer noch nicht.

In einem anderen Hinweis funktioniert eines meiner Relais nicht, wenn es an einen Pin (wenn alle Pins angeschlossen sind), aber es funktioniert nur, wenn einer der Pins getrennt ist. Es ist wirklich komisch. Ich testete das Relais und es ist in Ordnung, und ich änderte das Arduino, aber immer noch das gleiche Problem.

+0

ich die Tasten/Input Pins nehmen mit Masse verbunden, wenn sie nicht gedrückt, nicht wahr? Hast du dann alles auf einem Boden verbunden? Vielleicht, wenn man einen Knopf drückt, ist der Boden nicht mehr niedrig, weil der Strom fließt und die Pins vom Arduino als HIGH erkannt werden können. –

+0

Die Tasten sind alle mit der gleichen Masse verbunden. Der erste Knopf funktioniert wie ein Zauber, eigentlich perfekt und er reagiert sehr reaktiv, die anderen reagieren überhaupt nicht. Entschuldigung für die verspätete Antwort Ich hatte eine Sommerklasse Prüfung. –

Antwort

0

AH! Dies funktioniert hier ist der Code und die Schaltpläne für alle von Ihnen, die dieses Problem haben (sah im Internet eine Menge haben dieses Problem), sowie DANKE ALLES für Sie Hilfe bei der Lösung dieser Angelegenheit! GENIESSEN!

Circuit (Please note the relay cluster I am using was not available on Fritzing)

Code:

//Buttons 

int button1 = 7; 
int button2 = 6; 
int button3 = 4; 
int button4 = 2; 


//Relays 
int rl1 = 13; 
int rl2 = 12; 
int rl3 = 11; 
int rl4 = 8; 


//States for Relay and Button (1) 

int state1 = HIGH;  // the current state of the output pin 
int reading1;   // the current reading from the input pin 
int previous1 = LOW; // the previous reading from the input pin 

//States for Relay and Button (2) 

int state2 = HIGH;  // the current state of the output pin 
int reading2;   // the current reading from the input pin 
int previous2 = LOW; // the previous reading from the input pin 

//States for Relay and Button (3) 

int state3 = HIGH;  // the current state of the output pin 
int reading3;   // the current reading from the input pin 
int previous3 = LOW; // the previous reading from the input pin 

//States for Relay and Button (4) 

int state4 = HIGH;  // the current state of the output pin 
int reading4;   // the current reading from the input pin 
int previous4 = LOW; // the previous reading from the input pin 




// the follow variables are long's because the time, measured in miliseconds, 
// will quickly become a bigger number than can be stored in an int. 
long time1 = 0;   // the last time the output pin was toggled 
long time2 = 0; 
long time3 = 0; 
long time4 = 0; 

long debounce1 = 200; // the debounce time, increase if the output flickers 
long debounce2 = 200; 
long debounce3 = 200; 
long debounce4 = 200; 


void setup() 
{ 
    pinMode(button1, INPUT); 
    pinMode(button2, INPUT); 
    pinMode(button3, INPUT); 
    pinMode(button4, INPUT); 

    pinMode(rl1, OUTPUT); 
    pinMode(rl2, OUTPUT); 
    pinMode(rl3, OUTPUT); 
    pinMode(rl4, OUTPUT); 

} 

void loop() { 

    reading1 = digitalRead(button1); 
    reading2 = digitalRead(button2); 
    reading3 = digitalRead(button3); 
    reading4 = digitalRead(button4); 


    // if the input just went from LOW and HIGH and we've waited long enough 
    // to ignore any noise on the circuit, toggle the output pin and remember 
    // the time 
    //Condition Relay 1 
    if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) { 
    if (state1 == HIGH) 
     state1 = LOW; 
    else 
     state1 = HIGH; 

    time1 = millis();  
    } 

    //Condition Relay 2 
    if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) { 
    if (state2 == HIGH) 
     state2 = LOW; 
    else 
     state2 = HIGH; 

    time2 = millis();  
    } 

    //Condition Relay 3 
    if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) { 
    if (state3 == HIGH) 
     state3 = LOW; 
    else 
     state3 = HIGH; 

    time3 = millis();  
    } 

    //Condition Relay 4 
    if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce4) { 
    if (state4 == HIGH) 
     state4 = LOW; 
    else 
     state4 = HIGH; 

    time4 = millis();  
    } 




    digitalWrite(rl1, state1); 
    digitalWrite(rl2, state2); 
    digitalWrite(rl3, state3); 
    digitalWrite(rl4, state4); 


    previous1 = reading1; 
    previous2 = reading2; 
    previous3 = reading3; 
    previous4 = reading4; 
}