Att få led tejpen på eller av är inga problem, men jag vill att när man trycker på "on" på sändaren så skall led tejpen fedas upp och ner tills jag trycker "on" i gen från Nexa sändaren. Kodningen funkar var för sig, men hur skall jag få ihop det i slut programmet, det som händer nu är att varje gång jag trycker "on" så fedas den upp ett steg.
Har kämpat med detta nu någon vecka och får inte till, började bli väldigt snurrigt med all kod så jag började om helt, och nu så jag skulle behöva lite hjälp.
Denna fedar upp och ner tills jag trycker på knappen.
Kod: Markera allt
// this constant won't change:
const int buttonPin = 8; // the pin that the pushbutton is attached to
const int ledPin = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
}
void fade(){
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
} else {
// if the current state is LOW then the button
// wend from on to off:
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 2 == 0) {
analogWrite(ledPin, brightness);
delay(40);
// will change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
} else {
analogWrite(ledPin, brightness);
}
}
void loop() {
fade();
}
Här är koden med själva Nexan också utan fadekoden inlagd.
Kod: Markera allt
/*
* Demo for RF remote switch receiver.
* This example is for the new KaKu / Home Easy type of remotes!
*
* For details, see NewRemoteReceiver.h!
*
* With this sketch you can control a LED connected to digital pin 13,
* after the sketch learned the code. After start, the LED starts to blink,
* until a valid code has been received. The led stops blinking. Now you
* can control the LED with the remote.
*
* Note: only unit-switches are supported in this sketch, no group or dim.
*
* Set-up: connect the receiver to digital pin 2 and a LED to digital pin 13.
*/
#include <NewRemoteReceiver.h>
boolean codeLearned = false;
unsigned long learnedAddress;
unsigned short learnedUnit;
void setup() {
// LED-pin as output
pinMode(13, OUTPUT);
// Init a new receiver on interrupt pin 0, minimal 2 identical repeats, and callback set to processCode.
NewRemoteReceiver::init(0, 2, processCode);
}
void loop() {
// Blink led until a code has been learned
if (!codeLearned) {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
}
// Callback function is called only when a valid code is received.
void processCode(NewRemoteCode receivedCode) {
// A code has been received.
// Do we already know the code?
if (!codeLearned) {
// No! Let's learn the received code.
learnedAddress = receivedCode.address;
learnedUnit = receivedCode.unit;
codeLearned = true;
} else {
// Yes!
// Is the received code identical to the learned code?
if (receivedCode.address == learnedAddress && receivedCode.unit == learnedUnit) {
// Yes!
// switchType == 1 means on, switchType == 0 means off.
if (receivedCode.switchType == 1) {
digitalWrite(13, HIGH); //<<<<< Här är det TILL
} else if (receivedCode.switchType == 0) {
digitalWrite(13, LOW); //<<<<< Här är det FRÅN
}
}
}
}
Någon som kan komma på några idéer hur jag skall få ihop dessa?