26-10-2017, 05:22
Witam
Do przykładu z "Blink without Delay" wystarczy dodać swoje flagi sterujące i liczniki. Biblioteki robią to bardziej elegancko. W Twoim kodzie wystarczy zmienić zmienną int interval = 60000; i kod w tym warunku wykona się co 60s. Interwał powinien być tak dobrany, by można było sterować tym co ma być wykonane najczęściej, a resztę obrabiać licznikami. Np. sterowanie asynchronicznym miganiem trzema ledami:
Im więcej do zrobienia w programie tym bardziej się takie liczenie milisami rozjeżdża.
Do przykładu z "Blink without Delay" wystarczy dodać swoje flagi sterujące i liczniki. Biblioteki robią to bardziej elegancko. W Twoim kodzie wystarczy zmienić zmienną int interval = 60000; i kod w tym warunku wykona się co 60s. Interwał powinien być tak dobrany, by można było sterować tym co ma być wykonane najczęściej, a resztę obrabiać licznikami. Np. sterowanie asynchronicznym miganiem trzema ledami:
Kod:
/*
Blink without Delay
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.
The circuit:
- Use the onboard LED.
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
is set to the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your
Arduino model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
modified 11 Nov 2013
by Scott Fitzgerald
modified 9 Jan 2017
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to set a pin number:
const int ledPin1 = 11;// the number of the LED pin
const int ledPin2 = 12;// the number of the LED pin
const int ledPin3 = LED_BUILTIN;// the number of the LED pin
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 100; // interval at which to blink (milliseconds)
uint8_t flaga100MS,licznikMS, flaga1S, licznikS,flaga1M,licznikM;// flagi pokazują czy upłynęła ta jednostka czasu, liczniki je zliczają
void setup() {
Serial.begin(115200);
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
//zabawy z licznikami na poczatku loop
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
licznikMS++;
flaga100MS=1;
}
if(licznikMS>=10)
{
licznikMS=0;
licznikS++;
flaga1S=1;
}
if (licznikS>=60)
{
licznikS=0;
licznikM++;
flaga1M=1; //minuty się wyzerują po przepełnieniu zmiennej
}
//rzeczy do zrobienia
if(flaga100MS) //gdy minie 100ms
{
if(licznikMS%4==0) //to wykona sie co 400ms
{
digitalWrite(ledPin1,!(digitalRead(ledPin1)));//zmiana stanu wyjscia ledPin1
}
}
if(flaga1S) //gdy minie sekunda
{
Serial.print(licznikM);
Serial.print(":");
Serial.println(licznikS);
if(licznikS%3==0) //to wykona sie co 3s
{
digitalWrite(ledPin2,!(digitalRead(ledPin2)));//zmiana stanu wyjscia ledPin2
}
}
if(flaga1M) //gdy minie minuta
{
Serial.print("Mintka");
if(licznikM%2==0) //to wykona się co 2 minuty
{
digitalWrite(ledPin3,!(digitalRead(ledPin3)));//zmiana stanu wyjscia ledPin3
}
}
//zerujemy flagi na koniec petli loop
flaga100MS=0;flaga1S=0;flaga1M=0;
}
Miło być decenianym https://buycoffee.to/kaczakat