Ich übe ein bisschen mit dem Arduino (Ich bin normalerweise SPS- Programmierer)
Mein Problem: ich will die Instanz "blk" mehrfach aufrufen (ist ein simples Blinken-- Teilweise aus der Getting started)
// Used here to set a pin number :
const int Pin11 = 11; // the number of the pin
const int Pin12 = 12; // the number of the pin
// Variables :
//int pin_State = LOW;
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
// set the digital pin as output:
pinMode(Pin11, OUTPUT);
pinMode(Pin12, OUTPUT);
}
void loop() {
blk(2000,11) ;
blk(1000,12) ; }
void blk( const long interval, int Pin ) {
int pin_State;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// if the Pin is off turn it on and vice-versa:
if (pin_State == LOW) {
pin_State = HIGH;
} else {
pin_State = LOW;
}
// set the Pin with the Pin_State of the variable:
digitalWrite(Pin, pin_State);
Serial.print(currentMillis);Serial.print(" ");
;Serial.print(Pin);Serial.print(" ");
;Serial.print(interval);Serial.print(" ");
Serial.println(previousMillis);
}
}
Wobei?
Beim Lesen von "How to use this forum - please read"?
Beim Einfügen von Code-Tags, wenn Du Code in einem Beitrag postest?
Oder beim Code?
Bei Deinem Code kannst Du bei Deiner blk() Funktion natürlich nicht immer auf ein und dieselbe globale previousMillis Varriable Bezug nehmen, sondern Du brauchst für jedes Blinken eine eigene Variable, in der Du Dir den letzten Umschaltzeitpunkt merkst, z.B. previousMillis11 für das Blinken an Pin-11 und previousMillis12 für das Blinken an Pin-12. Die Variablen kannst Du wie auch die Pin-Nummer als Parameter an die Funktion übergeben.
Aber da es sich nicht um Konstanten handelt, sondern diese Variablen innerhalb der Funktion verändert werden sollen, kannst Du sie nicht einfach "by Value" übergeben, sondern die Übergabe muss "by Reference" erfolgen. Außerdem kannst Du Dir den pin_State nicht einfach aus dem freien Raum einer nicht-initialisierten lokalen Variablen herausziehen.
// Used here to set a pin number :
const int Pin11 = 11; // the number of the pin
const int Pin12 = 12; // the number of the pin
// Variables :
//int pin_State = LOW;
unsigned long previousMillis11, previousMillis12 ;
void setup() {
Serial.begin(9600);
// set the digital pin as output:
pinMode(Pin11, OUTPUT);
pinMode(Pin12, OUTPUT);
}
void loop() {
blk(2000,11, previousMillis11) ;
blk(1000,12, previousMillis12) ;
}
void blk( const long interval, int Pin, unsigned long &previousMillis ) {
int pin_State= digitalRead(Pin);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// if the Pin is off turn it on and vice-versa:
pin_State == !pin_State;
// set the Pin with the Pin_State of the variable:
digitalWrite(Pin, pin_State);
Serial.print(currentMillis);Serial.print(" ");
;Serial.print(Pin);Serial.print(" ");
;Serial.print(interval);Serial.print(" ");
Serial.println(previousMillis);
}
}
Dein etwas zu komplex geratenes Vertauschen des pin_State habe ich dabei gleich mal durch eine einfache Negation ersetzt.
Danke an Jurs!
Ich dachte der Code wäre erklärend genug mit dem was ich wollte.
und es ging nicht auf anhieb Code geändert jetzt läufts
// Used here to set a pin number :
const int Pin11 = 11; // the number of the pin
const int Pin12 = 12; // the number of the pin
// Variables :
//int pin_State = LOW;
unsigned long previousMillis11 = 0;
unsigned long previousMillis12 = 0;
void setup() {
Serial.begin(9600);
// set the digital pin as output:
pinMode(Pin11, OUTPUT);
pinMode(Pin12, OUTPUT);
}
void loop() {
blk(500,11, previousMillis11) ;
blk(1000,12, previousMillis12) ;
}
void blk( const long interval, int Pin, unsigned long &previousMillis ) {
int pin_State= digitalRead(Pin);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// pin_State == !pin_State;
// if the Pin is off turn it on and vice-versa:
if (pin_State == LOW) {
pin_State = HIGH;
} else {
pin_State = LOW;
}
// set the Pin with the Pin_State of the variable:
digitalWrite(Pin, pin_State);
Serial.print(currentMillis);Serial.print(" ");
;Serial.print(Pin);Serial.print(" ");
;Serial.print(interval);Serial.print(" ");
Serial.println(previousMillis);
}
}
pin_State == !pin_State;
das mag er irgend wie nicht
Frage: gibt es irgend wo eine Beschreibung über den Aufbau und Aufruf einer Instanz