ich habe vor ein 433Mhz Sender mit einem Button zu steuern. Dazu habe ich mir überlegt, dass wenn der Button gedrückt wurde, der Sender ein Signal ("An" an die Steckdose sendet) und sobald ich den Button das zweite mal betätige soll dann der Status auf AUS gehen. Ich habe auch schon ein bisschen experimentiert aber leider ohne Erfolg...
Wenn mir Jemand weiterhelfen kann, wäre das sehr nett!
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
const int buttonPin = 2;
int buttonState = 0; // current state of the button
int var = 0;
void setup() {
pinMode(buttonPin, INPUT);
mySwitch.enableTransmit(10);
}
void loop() {
if (buttonState == LOW) {
var = 1;
}
else {
var = 0;
}
if (var == 1) {
mySwitch.switchOn("11011", "10000");
} else {
mySwitch.switchOff("11011", "10000");
}
}
Habe bei mir mit Funk-Steckdosen die Erfahrung gemacht, dass nicht jedes An oder Aus Signal empfangen wird, sodass man dann mind. 2 weitere Mal drücken müste. Finde da 2 Taster besser und sende sicherheitshalber auch 3x je Taserdruck.
Viel Erfolg.
Ich habe nochmal eine kleine Nachfrage, wenn ich das mit mehreren Funksteckdosen über einen Sender machen möchte ist das dann so richtig vom Code? Oder geht das besser?
danke für weitere Hilfen!
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
int inPin = 2; // the number of the input pin
int inPin1 = 3; // the number of the input pin
int inPin2 = 4; // the number of the input pin
int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
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
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
// 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 time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// 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 debounce1 = 200; // the debounce time, increase if the output flickers
// 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 debounce2 = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
pinMode(inPin1, INPUT);
pinMode(inPin2, INPUT);
pinMode(outPin, OUTPUT);
mySwitch.enableTransmit(10);
}
void loop()
{
reading = digitalRead(inPin);
reading1 = digitalRead(inPin1);
reading2 = digitalRead(inPin2);
// // 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
// if (reading == HIGH && previous == LOW && millis() - time > debounce) {
// if (state == HIGH) {
// state = LOW;
// mySwitch.switchOn("11011", "10000");
// }
// else {
// state = HIGH;
// mySwitch.switchOff("11011", "10000");
// }
// time = millis();
// }
//
// digitalWrite(outPin, state);
//
// previous = reading;
if (reading == HIGH) {
// 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
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH) {
state = LOW;
mySwitch.switchOn("11011", "10000");
}
else {
state = HIGH;
mySwitch.switchOff("11011", "10000");
}
time = millis();
}
}
previous = reading;
///////////////////////////////////ggg
if (reading1 == HIGH) {
// 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
if (reading1 == HIGH && previous1 == LOW && millis() - time > debounce1) {
if (state1 == HIGH) {
state1 = LOW;
mySwitch.switchOn("11111", "11000");
}
else {
state1 = HIGH;
mySwitch.switchOff("11111", "11000");
}
time = millis();
}
}
previous1 = reading1;
///////////////////////////////////ggg
if (reading2 == HIGH) {
// 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
if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce2) {
if (state2 == HIGH) {
state2 = LOW;
mySwitch.switchOn("10000", "00001");
}
else {
state2 = HIGH;
mySwitch.switchOff("10000", "00001");
}
time = millis();
}
}
previous2 = reading2;
}
Du könntest Dir noch 8Byte sparen, indem Du debounce1 und 2 weg lässt und statt dessen auch debounce nutzt. Das muss nicht long sein sondern int oder byte reichen und da es konstant ist, kann sogar noch ein const davor, dann sparst Du insg. 12Byte Ram und 11 Flash. Das ist was mir fix aufgefallen ist. Sonst sieht es vernünftig aus, von dem was ich sagen kann. Funkioniert es wie gewünscht?
VG
Ps. Die Pins können auch alle const byte und die state previus und reading sind alle bool. Das spart Ram. Datür brauchst Du aber für jede der drei Funksteckdosen ein eigenes time also noch long time1 und 2, die dann auch entsprechend auf millis() gesetzt werden nach den jeweiligen if abfragen. Sonst funktioniert das debouncen nicht richtig.