Arduino - Projekte der 3AHET
Würden Sie gerne auf diese Nachricht reagieren? Erstellen Sie einen Account in wenigen Klicks oder loggen Sie sich ein, um fortzufahren.

Nebelmaschinensteuerung

Nach unten

Nebelmaschinensteuerung Empty Nebelmaschinensteuerung

Beitrag von PatrickEggi Do März 03, 2016 3:11 pm

Aufgabenstellung:
Nebelmaschinen Steuerung:
Umschaltbar zwischen zwei Modi:
• Nebelerkennung:
Bei zu starkem Nebel automatische Abschaltung – (Pause 2sek)

• Timerfunktion:
Einstellbare Zeit in dem die Nebelmaschine anläuft
Eingestellter Modus erkennbar an LED.
Nebelerkennung:        Orange
Timerfunktion: Blau




Derzeitige Probleme:
In Modus "Timer" nicht umschaltbar auf Modus "Nebelerkennung" umgekehrt schon
Zeit im Timer-Modus derzeit nur über Code einstellbar
mehrere Versuche hingegen dauerhafter Nebel bei gedrückten Button (derzeit nur teilweise möglich)


Code:

#define debounce 20
#define holdTime 2000

int Ledr = 12;
int Ledg = 13;
int Ledb = 11;
int lightsensor = 2;
int val;
int val2;
int val3;
int ONOFF = 4;
int Led2r = 7;
int Led2g = 6;
int Led2b = 5;
int button = 18;
int Fog = 3;
int buttonVal;
int buttonLast;
int LED=9;
int Timer = 8;

 
long btnDnTime;
long btnUpTime;
boolean ignoreUp = false;

int buttonState;         // current state of the button
int lastButtonState;     // previous state of the button
int buttonPushCounter;

double x, y, delta;
int mappedBrightness;
const double TWOPI = 6.283185;
const int MAXBRIGHTNESS = 255; // max allowed MAXBRIGHTNESS: 255
const int TEMPO = 5; // ms after each analogWrite()
                      // a value of 15 may be like Apple's glowing


void setup() {
  // put your setup code here, to run once:
pinMode(Ledr, OUTPUT);
pinMode(Ledg, OUTPUT);
pinMode(Ledb, OUTPUT);
pinMode(lightsensor, INPUT);
pinMode (Led2r,OUTPUT);
pinMode (Led2g,OUTPUT);
pinMode (Led2b,OUTPUT);
pinMode (button,INPUT);
pinMode (ONOFF,INPUT);
pinMode (Fog, OUTPUT);
pinMode (Timer,OUTPUT);


attachInterrupt(digitalPinToInterrupt(5),durableFog , RISING);

Serial.begin(9600);

}
void loop()
{
  // put your main code here, to run repeatedly:
val = digitalRead (lightsensor);
val3 = digitalRead (ONOFF);
buttonState = digitalRead(button);




if (buttonState == HIGH)                                                  //Button Zustandsabfrage bei gedrücktem Knopf -> Timecheck
{

          buttonVal=digitalRead(button);
          
              if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnUpTime) > long(debounce))
              {
              btnDnTime = millis();
              }
              
              // Test for button release and store the up time
              if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnDnTime) > long(debounce))
              {
              if (ignoreUp == false) event1();
              else ignoreUp = false;
              btnUpTime = millis();
              }
              
              // Test for button held down for longer than the hold time
              if (buttonVal == HIGH && (millis() - btnDnTime) > long(holdTime))
              {
              event2();
              ignoreUp = true;
              btnDnTime = millis();
              }
              
              buttonLast = buttonVal;
    
}
else                                                                            //Nicht gedrückter Button -> Programm läuft bzw check Buttonstatus und schaltet gegebenenfalls um
{
  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++;
                  Serial.println("on");
                  Serial.print("number of button pushes:  ");
                  Serial.println(buttonPushCounter);
                } else {
                  // if the current state is LOW then the button
                  // wend from on to off:
                  Serial.println("off");
                }
                // Delay a little bit to avoid bouncing
              
              }
              // save the current state as the last state,
              //for next time through the loop
              lastButtonState = buttonState;
            
            
              if (buttonPushCounter % 2 == 0)
              {
                                                                                   // Nebelerkennung
                              analogWrite (Ledr,255);
                              analogWrite (Ledg,30);
                              analogWrite (Ledb,0);
                        
                        if (val == LOW)
                          {
                              digitalWrite (Fog,LOW);
                              digitalWrite (Led2r,HIGH);
                              digitalWrite (Led2b,LOW);
                              delay (5000);
                              
                          }
                        else
                          {
                    
                              digitalWrite (Fog,HIGH);
                              digitalWrite (Led2b,HIGH);
                              digitalWrite (Led2r,LOW);
                          }
              }
             else
              {
                                                                                            //Timer
                        digitalWrite (Ledr,LOW);
                        digitalWrite (Ledg,LOW);
                        digitalWrite (Ledb,HIGH);
                        digitalWrite (Fog,HIGH);
                        delay (2000);
                        digitalWrite (Ledr,LOW);
                        digitalWrite (Ledg,LOW);
                        digitalWrite (Ledb,HIGH);
                        digitalWrite (Fog,LOW);
                        delay (2000);
              }
}
    
    }
    
    
    
    void event1()
      {  
              
              if (buttonState != lastButtonState) {
                if (buttonState == HIGH) {

                  buttonPushCounter++;
                  Serial.println("on");
                  Serial.print("number of button pushes:  ");
                  Serial.println(buttonPushCounter);
                } else {

                  Serial.println("off");
                }
                delay(50);
              }
              lastButtonState = buttonState;
            
            
              if (buttonPushCounter % 2 == 0)
              {
                 // Nebelerkennung
                              analogWrite (Ledr,255);
                              analogWrite (Ledg,30);
                              analogWrite (Ledb,0);
                              digitalWrite (Timer,LOW);
                        
                        if (val == LOW)
                          {
                              digitalWrite (Fog,LOW);
                              digitalWrite (Led2r,HIGH);
                              digitalWrite (Led2b,LOW);
                              delay (5000);
                              
                          }
                        else
                          {
                    
                              digitalWrite (Fog,HIGH);
                              digitalWrite (Led2b,HIGH);
                              digitalWrite (Led2r,LOW);
                          }
              }
             else
              {
                 //Timer
                         digitalWrite (Ledr,LOW);
                        digitalWrite (Ledg,LOW);
                        digitalWrite (Ledb,HIGH);
                        digitalWrite (Fog,HIGH);
                        delay (2000);
                        digitalWrite (Ledr,LOW);
                        digitalWrite (Ledg,LOW);
                        digitalWrite (Ledb,HIGH);
                        digitalWrite (Fog,LOW);
                        delay (2000);
                        
                        
              }  
      }
    
      void event2()
      {
        if (buttonVal == HIGH)
          {
             digitalWrite (Ledr,LOW);
                        digitalWrite (Ledg,LOW);
                        digitalWrite (Ledb,HIGH);
                        digitalWrite (Fog,HIGH);
                        delay (2000);
                        digitalWrite (Ledr,LOW);
                        digitalWrite (Ledg,LOW);
                        digitalWrite (Ledb,HIGH);
                        digitalWrite (Fog,LOW);
                        delay (2000);
            
            
            

              x = 0;
  delta = TWOPI / MAXBRIGHTNESS;

  double halfMaxBrightness = MAXBRIGHTNESS / 2.0;

  while (x < TWOPI) {
    y = sin(x);
    mappedBrightness = halfMaxBrightness*y + halfMaxBrightness;
    analogWrite(Ledb, mappedBrightness);
    delay(TEMPO / (MAXBRIGHTNESS / 255.0));
    x += delta;
  }
            
            
            
          }
  }
  void durableFog()
{

      while(buttonState==HIGH)
      {      
            
            
            
            
            
            digitalWrite(Fog,HIGH);
            digitalWrite(Led2b,HIGH);
            digitalWrite(Led2r,LOW);
            digitalWrite(Led2g,LOW);
            digitalWrite(Ledr,LOW);
            digitalWrite(Ledg,LOW);
            
            
            

              x = 0;
              delta = TWOPI / MAXBRIGHTNESS;
            
              double halfMaxBrightness = MAXBRIGHTNESS / 2.0;

          while (x < TWOPI)
          {
            y = sin(x);
            mappedBrightness = halfMaxBrightness*y + halfMaxBrightness;
            analogWrite(Ledb, mappedBrightness);
            delay(TEMPO / (MAXBRIGHTNESS / 255.0));
            x += delta;
          }
      }  
            


}





Nebelmaschinensteuerung Untitl11
PatrickEggi
PatrickEggi

Anzahl der Beiträge : 1
Anmeldedatum : 24.02.16

Nach oben Nach unten

Nach oben


 
Befugnisse in diesem Forum
Sie können in diesem Forum nicht antworten