Demo entry 6356625

homework

   

Submitted by anonymous on Apr 19, 2017 at 14:27
Language: C++. Code size: 2.9 kB.

int value;
int brightness = 0;
int fadeAmount = 5;
void setup() {
  // put your setup code here, to run once:
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
  digitalWrite(10,HIGH);
  digitalWrite(9, HIGH); 
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:


  if(Serial.available()> 0){
    value = Serial.read();
  }
  if(value != -1){
    Serial.println(value);
    
    switch (value){
      case '1':
          pwmAll();
          break;
      case '2':
        closeAll();
        break;
      case '3':
        lightAll();
        break;
      case '4':
          mar();
          break;
      case '5':
          mar2();
          break;
  }
}
//正向跑马灯
void mar(){
            digitalWrite(10,HIGH);
            delay(50);
            digitalWrite(10,LOW);
            delay(50);
            
            digitalWrite(9,HIGH);
            delay(50);
            digitalWrite(9,LOW);
            delay(50);

            
            digitalWrite(5,HIGH);
            delay(50);
            digitalWrite(5,LOW);
            delay(50);
            
            digitalWrite(6,HIGH);
            delay(50);
            digitalWrite(6,LOW);
            delay(50);
            
}
//逆向跑马灯
void mar2(){
  
            digitalWrite(10,HIGH);
            delay(50);
            digitalWrite(10,LOW);
            delay(50);
            
            digitalWrite(6,HIGH);
            delay(50);
            digitalWrite(6,LOW);
            delay(50);

            digitalWrite(5,HIGH);
            delay(50);
            digitalWrite(5,LOW);
            delay(50);
            
            digitalWrite(9,HIGH);
            delay(50);
            digitalWrite(9,LOW);
            delay(50);

            
            
            
}
//所有的小灯同时呼吸(PWM)
void pwmAll(){
          analogWrite(10, brightness);
          analogWrite(9, brightness);
          analogWrite(6, brightness);
          analogWrite(5, brightness);
          brightness = brightness + fadeAmount;
          if(brightness == 0 || brightness == 255){
            fadeAmount = - fadeAmount;
          }
          delay(50);
}
//熄灭所有小灯
void closeAll(){
          digitalWrite(10,LOW);
          digitalWrite(9,LOW);
          digitalWrite(6,LOW);
          digitalWrite(5,LOW);
}
//点亮所有小灯
void lightAll(){
  
          digitalWrite(10,HIGH);
          digitalWrite(9,HIGH);
          digitalWrite(6,HIGH);
          digitalWrite(5,HIGH);
}

//使单独某个小灯呼吸
void pwm(int color){
  analogWrite(color, brightness);
  brightness = brightness + fadeAmount;
  if(brightness == 0 || brightness == 255){
    fadeAmount = - fadeAmount;
  }
  delay(50);
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).