Arduino RGB Blinker Final

From S23Wiki
Jump to: navigation, search

text later

const int  buttonPin = 10;    // the pin that the pushbutton is attached to
const int ledPin1 = 13;       // the pin that the LED is attached to
const int ledPin2 = 12; 
const int ledPin3 = 11; 
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

int sensorPin1 = 0;    // select the input pin for the potentiometer
int sensorValue1 = 0;  // variable to store the value coming from the sensor
int sensorMin1 = 10000;  // minimum sensor value
int sensorMax1 = 0;     // maximum sensor value
int sensorPin2 = 1;    // select the input pin for the potentiometer
int sensorValue2= 0;  // variable to store the value coming from the sensor
int sensorMin2 = 10000;  // minimum sensor value
int sensorMax2 = 0;     // maximum sensor value
int sensorPin3 = 2;    // select the input pin for the potentiometer
int sensorValue3= 0;  // variable to store the value coming from the sensor
int sensorMin3 = 10000;  // minimum sensor value
int sensorMax3 = 0;     // maximum sensor value

int  LED1 = 9;
int  LED2 = 8;
int  LED3 = 7;
int  LED4 = 6;
int  LED5 = 5;
int  LED6 = 4; 
int  LED7 = 3;
int  LED8 = 2;
int rgb1;
int rgb2;




void setup() {

  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds 
  while (millis() < 5000) {
    sensorValue1 = analogRead(sensorPin1);

    // record the maximum sensor value
    if (sensorValue1 > sensorMax1) {
      sensorMax1 = sensorValue1;
    }

    // record the minimum sensor value
    if (sensorValue1 < sensorMin1) {
      sensorMin1 = sensorValue1;
    }
    sensorValue2 = analogRead(sensorPin2);
    // record the maximum sensor value
    if (sensorValue2 > sensorMax2) {
      sensorMax2 = sensorValue2;
    }

    // record the minimum sensor value
    if (sensorValue2 < sensorMin2) {
      sensorMin2 = sensorValue2;
    }
    sensorValue3 = analogRead(sensorPin3);

    // record the maximum sensor value
    if (sensorValue3 > sensorMax3) {
      sensorMax3 = sensorValue3;
    }

    // record the minimum sensor value
    if (sensorValue3 < sensorMin3) {
      sensorMin3 = sensorValue3;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW); 
}
void r1() {      
  digitalWrite(ledPin1,HIGH);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledPin3,LOW);
}
void r2() {      
  digitalWrite(ledPin1,LOW);
  digitalWrite(ledPin2,HIGH);
  digitalWrite(ledPin3,LOW);
}
void r3() {      
  digitalWrite(ledPin1,LOW);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledPin3,HIGH);
}
void r12() {      
  digitalWrite(ledPin1,HIGH);
  digitalWrite(ledPin2,HIGH);
  digitalWrite(ledPin3,LOW);
}
void r13() {      
  digitalWrite(ledPin1,HIGH);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledPin3,HIGH);
}
void r23() {      
  digitalWrite(ledPin1,LOW);
  digitalWrite(ledPin2,HIGH);
  digitalWrite(ledPin3,HIGH);
}
void r123() {      
  digitalWrite(ledPin1,HIGH);
  digitalWrite(ledPin2,HIGH);
  digitalWrite(ledPin3,HIGH);
}
void r0() {      
  digitalWrite(ledPin1,LOW);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledPin3,LOW);
}
void RGBgelb1() {      
  digitalWrite(LED1,HIGH);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,LOW);
}
void RGBgelb2() {      
  digitalWrite(LED5,HIGH);
  digitalWrite(LED6,LOW);
  digitalWrite(LED7,HIGH);
  digitalWrite(LED8,LOW);
}
void RGBlila1() {      
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,HIGH);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,LOW);
}
void RGBlila2() {      
  digitalWrite(LED5,LOW);
  digitalWrite(LED6,HIGH);
  digitalWrite(LED7,HIGH);
  digitalWrite(LED8,LOW);
}
void RGBblau1() {      
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,HIGH);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,HIGH);
}
void RGBblau2() {      
  digitalWrite(LED5,LOW);
  digitalWrite(LED6,HIGH);
  digitalWrite(LED7,HIGH);
  digitalWrite(LED8,HIGH);
}
void RGBrot1() {      
  digitalWrite(LED1,HIGH);
  digitalWrite(LED2,HIGH);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,LOW);
}
void RGBrot2() {      
  digitalWrite(LED5,HIGH);
  digitalWrite(LED6,HIGH);
  digitalWrite(LED7,HIGH);
  digitalWrite(LED8,LOW);
}
void RGBgruen1() {      
  digitalWrite(LED1,HIGH);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,HIGH);
}
void RGBgruen2() {      
  digitalWrite(LED5,HIGH);
  digitalWrite(LED6,LOW);
  digitalWrite(LED7,HIGH);
  digitalWrite(LED8,HIGH);
}
void RGBtuerkis1() {      
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,HIGH);
}
void RGBtuerkis2() {      
  digitalWrite(LED5,LOW);
  digitalWrite(LED6,LOW);
  digitalWrite(LED7,HIGH);
  digitalWrite(LED8,HIGH);
}
void RGBweiss1() {      
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,HIGH);
  digitalWrite(LED4,LOW);
}
void RGBweiss2() {      
  digitalWrite(LED5,LOW);
  digitalWrite(LED6,LOW);
  digitalWrite(LED7,HIGH);
  digitalWrite(LED8,LOW);
}
void RGBaus1() {      
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);
  digitalWrite(LED4,LOW);
}
void RGBaus2() {      
  digitalWrite(LED5,LOW);
  digitalWrite(LED6,LOW);
  digitalWrite(LED7,LOW);
  digitalWrite(LED8,LOW);
}
void loop() {


  // read the sensor1
  sensorValue1 = analogRead(sensorPin1);
  // apply the calibration to the sensor reading
  sensorValue1 = map(sensorValue1, sensorMin1, sensorMax1, 0, 1000);
  // in case the sensor value is outside the range seen during calibration
  sensorValue1 = constrain(sensorValue1, 0, 1000);

  // read the sensor2
  sensorValue2 = analogRead(sensorPin2);
  // apply the calibration to the sensor reading
  sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, 0, 1000);
  // in case the sensor value is outside the range seen during calibration
  sensorValue2 = constrain(sensorValue2, 0, 1000);

  // read the sensor3
  sensorValue3 = analogRead(sensorPin3);
  // apply the calibration to the sensor reading
  sensorValue3 = map(sensorValue3, sensorMin3, sensorMax3, 0, 255);
  // in case the sensor value is outside the range seen during calibration
  sensorValue3 = constrain(sensorValue3, 0, 255);
  // read the pushbutton input pin:
  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++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }

    // save the current state as the last state, 
    //for next time through the loop
    lastButtonState = buttonState;
  }
  /////// eins
  if (buttonPushCounter == 1) {
    r1();
    rgb1 = (random (1, 7)); 
  delay(sensorValue1);
  switch (rgb1)         
  {
  case 1:
    RGBlila1();
    RGBlila2();
    break;
  case 2:
    RGBblau1();
    RGBblau2();
    break;
  case 3:
    RGBtuerkis1();
    RGBtuerkis2();
    break;
  case 4:
    RGBgruen1();
    RGBgruen2();
    break;
  case 5:
    RGBgelb1();
    RGBgelb2();
    break;
  case 6:
    RGBrot1();
    RGBrot2();
    break;
  case 7:
    RGBweiss1();
    RGBweiss2();
    break;
  default: 
    RGBaus1();
    RGBaus2();
    break;

  }

  }
  /////// zwei
  if (buttonPushCounter  == 2) {
    r2();
    rgb1 = (random (1, 7)); 
  delay(sensorValue1);
  switch (rgb1)         
  {
  case 1:
    RGBlila1();
    delay(sensorValue2);
    RGBlila2();
    break;
  case 2:
    RGBblau1();
    delay(sensorValue2);
    RGBblau2();
    break;
  case 3:
    RGBtuerkis1();
    delay(sensorValue2);
    RGBtuerkis2();
    break;
  case 4:
    RGBgruen1();
    delay(sensorValue2);
    RGBgruen2();
    break;
  case 5:
    RGBgelb1();
    delay(sensorValue2);
    RGBgelb2();
    break;
  case 6:
    RGBrot1();
    delay(sensorValue2);
    RGBrot2();
    break;
  case 7:
    RGBweiss1();
    delay(sensorValue2);
    RGBweiss2();
    break;
  default: 
    RGBaus1();
    RGBaus2();
    break;

  }
  }   
  /////// drei
  if (buttonPushCounter  == 3) {
    r3();
    for (rgb1 = 1; rgb1 < 8; rgb1++){

  delay(sensorValue1);
  switch (rgb1)         
  {
  case 1:
    RGBlila1();
    delay(sensorValue2);
    RGBlila2();
    break;
  case 2:
    RGBblau1();
    delay(sensorValue2);
    RGBblau2();
    break;
  case 3:
    RGBtuerkis1();
    delay(sensorValue2);
    RGBtuerkis2();
    break;
  case 4:
    RGBgruen1();
    delay(sensorValue2);
    RGBgruen2();
    break;
  case 5:
    RGBgelb1();
    delay(sensorValue2);
    RGBgelb2();
    break;
  case 6:
    RGBrot1();
    delay(sensorValue2);
    RGBrot2();
    break;
  case 7:
    RGBweiss1();
    delay(sensorValue2);
    RGBweiss2();
    break;
  default: 
    RGBaus1();
    RGBaus2();
    break; }

  }
  }
  if (buttonPushCounter  == 4) {
    r12();
  sensorValue1 = analogRead(sensorPin1);

  // apply the calibration to the sensor reading
  sensorValue1 = map(sensorValue1, sensorMin1, sensorMax1, 0, 7);

  // in case the sensor value is outside the range seen during calibration
  sensorValue1 = constrain(sensorValue1, 0, 7);
  sensorValue2 = analogRead(sensorPin2);

  // apply the calibration to the sensor reading
  sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, 0, 7);

  // in case the sensor value is outside the range seen during calibration
  sensorValue2 = constrain(sensorValue2, 0, 7);
 
 

   rgb1 = (sensorValue1); 
  switch (rgb1)         
  {
  case 1:
    RGBgelb1();
    break;
  case 2:
    RGBlila1();
    break;
  case 3:
    RGBblau1();
    break;
  case 4:
    RGBrot1();
    break;
  case 5:
    RGBgruen1();
    break;
  case 6:
    RGBtuerkis1();
    break;
  case 7:
    RGBweiss1();
    break;
  default: 
    RGBaus1();
    break;

  }
 rgb2 = (sensorValue2);
  switch (rgb2)         
  {
  case 1:
    RGBgelb2();
    break;
  case 2:
    RGBlila2();
    break;
  case 3:
    RGBblau2();
    break;
  case 4:
    RGBrot2();
    break;
  case 5:
    RGBgruen2();
    break;
  case 6:
    RGBtuerkis2();
    break;
  case 7:
    RGBweiss2();
    break;
  default: 
    RGBaus2();
    break;

  }
  }
  if (buttonPushCounter  == 5) {
      sensorValue1 = analogRead(sensorPin1);

  // apply the calibration to the sensor reading
  sensorValue1 = map(sensorValue1, sensorMin1, sensorMax1, 0, 7);

  // in case the sensor value is outside the range seen during calibration
  sensorValue1 = constrain(sensorValue1, 0, 7);
  sensorValue2 = analogRead(sensorPin2);

  // apply the calibration to the sensor reading
  sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, 0, 7);

  // in case the sensor value is outside the range seen during calibration
  sensorValue2 = constrain(sensorValue2, 0, 7);
   // read the sensor3
  sensorValue3 = analogRead(sensorPin3);
  // apply the calibration to the sensor reading
  sensorValue3 = map(sensorValue3, sensorMin3, sensorMax3, 0, 255);
  // in case the sensor value is outside the range seen during calibration
  sensorValue3 = constrain(sensorValue3, 0, 255);
  
    r13();
    delay(sensorValue3);
    RGBaus2();
    
    rgb1 = (sensorValue1); 
  switch (rgb1)         
  {
  case 1:
    RGBgelb1();
    break;
  case 2:
    RGBlila1();
    break;
  case 3:
    RGBblau1();
    break;
  case 4:
    RGBrot1();
    break;
  case 5:
    RGBgruen1();
    break;
  case 6:
    RGBtuerkis1();
    break;
  case 7:
    RGBweiss1();
    break;
  default: 
    RGBaus1();
    break;

  }
  delay(sensorValue3);
      RGBaus1();
    
 rgb2 = (sensorValue2);
  switch (rgb2)         
  {
  case 1:
    RGBgelb2();
    break;
  case 2:
    RGBlila2();
    break;
  case 3:
    RGBblau2();
    break;
  case 4:
    RGBrot2();
    break;
  case 5:
    RGBgruen2();
    break;
  case 6:
    RGBtuerkis2();
    break;
  case 7:
    RGBweiss2();
    break;
  default: 
    RGBaus2();
    break;

  }
  }
  if (buttonPushCounter  == 6) {
    r23();
      sensorValue1 = analogRead(sensorPin1);

  // apply the calibration to the sensor reading
  sensorValue1 = map(sensorValue1, sensorMin1, sensorMax1, 0, 150);

  // in case the sensor value is outside the range seen during calibration
  sensorValue1 = constrain(sensorValue1, 0, 150);

  sensorValue2 = analogRead(sensorPin2);

  // apply the calibration to the sensor reading
  sensorValue2 = map(sensorValue2, sensorMin2, sensorMax2, 0, 150);

  // in case the sensor value is outside the range seen during calibration
  sensorValue2 = constrain(sensorValue2, 0, 150);

  sensorValue3 = analogRead(sensorPin3);

  // apply the calibration to the sensor reading
  sensorValue3 = map(sensorValue3, sensorMin3, sensorMax3, 0, 255);

  // in case the sensor value is outside the range seen during calibration
  sensorValue3 = constrain(sensorValue3, 0, 255);

  delay(sensorValue3); 
  rgb1 = (random (sensorValue1, sensorValue2)); 
  switch (rgb1)         
  {
  case 1:
    RGBgelb1();
    break;
  case 2:
    RGBlila1();
    break;
  case 3:
    RGBblau1();
    break;
  case 4:
    RGBrot1();
    break;
  case 5:
    RGBgruen1();
    break;
  case 6:
    RGBtuerkis1();
    break;
  case 7:
    RGBweiss1();
    break;
  default: 
    RGBaus1();
    break;

  }

delay(random(sensorValue1, sensorValue2)); 
rgb1 = (random (1, 7));
 switch (rgb1)         
  {
 case 1:
   RGBgelb1();
break;
case 2:
RGBlila1();
break;
case 3:
RGBblau1();
break;
case 4:
RGBrot1();
break;
case 5:
RGBgruen1();
break;
case 6:
RGBtuerkis1();
break;
case 7:
RGBweiss1();
break;
default: 
RGBaus1();
break;

  }

  delay(sensorValue3); 
  rgb2 =  (random (sensorValue1, sensorValue2)); 
  switch (rgb2)         
  {
  case 1:
    RGBgelb2();
    break;
  case 2:
    RGBlila2();
    break;
  case 3:
    RGBblau2();
    break;
  case 4:
    RGBrot2();
    break;
  case 5:
    RGBgruen2();
    break;
  case 6:
    RGBtuerkis2();
    break;
  case 7:
    RGBweiss2();
    break;
  default: 
    RGBaus2();
    break;

  }

delay(random(sensorValue1, sensorValue2)); 
  rgb2 = (random (1, 7));
  switch (rgb2)         
  {
  case 1:
    RGBgelb2();
    break;
  case 2:
    RGBlila2();
    break;
  case 3:
    RGBblau2();
    break;
  case 4:
    RGBrot2();
    break;
  case 5:
    RGBgruen2();
    break;
  case 6:
    RGBtuerkis2();
    break;
  case 7:
    RGBweiss2();
    break;
  default: 
    RGBaus2();
    break;
  }

  }
  if (buttonPushCounter  == 7) {
    r123();
  }
  if (buttonPushCounter  == 8) {
    r0();
    buttonPushCounter = 0;
  }
}