Walka z kodem cd - grzesio.px - 26-01-2020
Witam mam znów za pewnie błahy problem a mianowicie chciałem do tematu huśtawki dołożyć włącznik temat udało się (niby) ogarnąć napisałem kawałek kodu ale drganie styków całą zabawę rozpierniczyło. Przeszukałem forum arduino.cc i znalazłem przykład jak przy użyciu funkcji debounce temat ogarnąć. I tu zaczynają się schody nie mogę wykombinować w którym miejscu umieścić program huśtawki (który działa super) żeby po "wyłączeniu" czyli port tranzystora LOW program przestał zmieniać stany portów 8 i 10
Kod: unsigned long miganie2 = 355;
unsigned long miganie3 = 699;
unsigned long zapamietanyCzas2 = 0;
unsigned long zapamietanyCzas3 = 0;
byte stan = 0;
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
// Variables will change:
int ledState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
if (millis() - zapamietanyCzas2 >= miganie2 && stan == 0)
{
digitalWrite(8, LOW);
digitalWrite(10, HIGH);
stan = 1;
zapamietanyCzas3 = millis();
}
if (millis() - zapamietanyCzas3 >= miganie3 && stan == 1)
{
digitalWrite(8, HIGH);
digitalWrite(10, LOW);
stan = 0;
zapamietanyCzas2 = millis();
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
RE: Walka z kodem cd - Agregacik - 27-01-2020
Spróbuj tego, może trzeba odwrócić logikę bo nie wiem kiedy świeci dioda.
Kod: unsigned long miganie2 = 355;
unsigned long miganie3 = 699;
unsigned long zapamietanyCzas2 = 0;
unsigned long zapamietanyCzas3 = 0;
byte stan = 0;
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
// Variables will change:
int ledState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)
{
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH)
{
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
if (ledState == LOW)
{
if (millis() - zapamietanyCzas2 >= miganie2 && stan == 0)
{
digitalWrite(8, LOW);
digitalWrite(10, HIGH);
stan = 1;
zapamietanyCzas3 = millis();
}
if (millis() - zapamietanyCzas3 >= miganie3 && stan == 1)
{
digitalWrite(8, HIGH);
digitalWrite(10, LOW);
stan = 0;
zapamietanyCzas2 = millis();
}
}
if (ledState == HIGH)
{
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
RE: Walka z kodem cd - kataklysm - 27-01-2020
(26-01-2020, 14:20)grzesio.px napisał(a): Witam mam znów za pewnie błahy problem a mianowicie chciałem do tematu huśtawki dołożyć włącznik temat udało się (niby) ogarnąć napisałem kawałek kodu ale drganie styków całą zabawę rozpierniczyło. Przeszukałem forum arduino.cc i znalazłem przykład jak przy użyciu funkcji debounce temat ogarnąć. I tu zaczynają się schody nie mogę wykombinować w którym miejscu umieścić program huśtawki (który działa super) żeby po "wyłączeniu" czyli port tranzystora LOW program przestał zmieniać stany portów 8 i 10
Kod: unsigned long miganie2 = 355;
unsigned long miganie3 = 699;
unsigned long zapamietanyCzas2 = 0;
unsigned long zapamietanyCzas3 = 0;
byte stan = 0;
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
// Variables will change:
int ledState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(8, HIGH);
digitalWrite(10, HIGH);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
if (millis() - zapamietanyCzas2 >= miganie2 && stan == 0)
{
digitalWrite(8, LOW);
digitalWrite(10, HIGH);
stan = 1;
zapamietanyCzas3 = millis();
}
if (millis() - zapamietanyCzas3 >= miganie3 && stan == 1)
{
digitalWrite(8, HIGH);
digitalWrite(10, LOW);
stan = 0;
zapamietanyCzas2 = millis();
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
Kod: pinMode(buttonPin, INPUT);
Zmień to na pinMode(buttonPin, INPUT_PULLUP);
to właściwie powinno Ci wystarczyć dopisz sobie metodę z licznikiem która zablokuje stan buttonPin na jakiś krótki czas po naciśnięciu, to powinno wyeliminować efekt drgań styków.
RE: Walka z kodem cd - Agregacik - 27-01-2020
@kataklysm, jego program eliminacji drgania styków bardzo dobrze działa, zapala i gasi ładnie diodę. Dodałem tylko if y aby huśtawka uruchamiałe się, gdy dioda świeci i zatrzymywała jak nie świeci. Sprawdziłem na modelu "diodkowym". No tak masz rację trzeba dać podciągnięcie, ja dałem zewnętrzny oporniczek.
RE: Walka z kodem cd - grzesio.px - 27-01-2020
Dzięki chłopaki ratujecie mi kolejny raz życie. Pozdrawiam
|