שיעור 11 - לולאת FOR

ברוכים הבאים לשיעור!

השיעור מורכב מסדרה של סרטונים, צפו בהם לפי הסדר.

תהנו 🙂

חלק 1: הקדמה

חלק 2: לולאת FOR

העתיקו את הקוד מקופסה זו:

  #include <FastLED.h>
  #define NUM_LEDS 16
  #define DATA_PIN 33
  CRGB leds[NUM_LEDS];

  int counter = 0;
 
void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  //LEDs ON
  while (counter<16)
  {
    leds[counter] = CRGB::Blue;
    FastLED.show();
    delay(50);
    counter = counter + 1;
  }
 
}

void loop() {

} 

חלק 3: איך נבחר באיזו לולאה להשתמש?

העתיקו את הקוד מקופסה זו:

  
  //FastLED
  #include <FastLED.h>
  #define NUM_LEDS 16
  #define DATA_PIN 33
  CRGB leds[NUM_LEDS];
 
  //Ultrasonic
  int trigPin = 12;    // Trigger
  int echoPin = 35;    // Echo
  long duration, inches, cm=0;
 
void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {

   //Read ultrasonic
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   cm = duration / 29 / 2;
   
   while (cm<15)
   {
      //=========Start blinking============
      for(int i=0; i<16; i++) {
         leds[i] = CRGB::Red;
      }        
      FastLED.show();  
      delay(250);
     
      for(int i=0; i<16; i++) {
         leds[i] = CRGB::Black;
      }    
      FastLED.show();  
      delay(250);
      //=========End Blinking===============
       
     //Read ultrasonic again
     digitalWrite(trigPin, LOW);
     delayMicroseconds(2);
     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     duration = pulseIn(echoPin, HIGH);
     cm = duration / 29 / 2;
   }  
}
 

חלק 4: הדפסת ספרות

העתיקו את הקוד מקופסה זו:

  #include <FastLED.h>
  #define NUM_LEDS 16
  #define DATA_PIN 33
  CRGB leds[NUM_LEDS];
 
void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  leds[0] = CRGB::Red;
  FastLED.show();
}

void loop() {

}
 

חלק 5: הצגת כל הספרות

//FastLED
  #include <FastLED.h>
  #define NUM_LEDS 16
  #define DATA_PIN 33
  CRGB leds[NUM_LEDS];

void setup() {
    //FastLED setup
    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
    FastLED.setBrightness(50);

    leds[2] = CRGB::Red;
    leds[5] = CRGB::Red;
    leds[6] = CRGB::Red;
    leds[10] = CRGB::Red;
    leds[13] = CRGB::Red;      
    FastLED.show();  
    delay(1000);

    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[6] = CRGB::Red;
    leds[9] = CRGB::Red;
    leds[13] = CRGB::Red;      
    leds[14] = CRGB::Red;  
    leds[15] = CRGB::Red;  
    FastLED.show();    
}


void loop() {


}
 

בונוס: הדפסת מרחק

  //FastLED
  #include <FastLED.h>
  #define NUM_LEDS 16
  #define DATA_PIN 33
  CRGB leds[NUM_LEDS];

  //Ultrasonic Setup
  int trigPin = 12;    // Trigger
  int echoPin = 35;    // Echo
 
void setup() {
  //FastLED setup
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  Serial.begin(9600);
}


 // 0-10 | 1
 // 10-20 | 2
 // 20-30 | 3
 // 30-40 | 4
 // 40-50 | 5
 // 50-60 | 6
 // 60-70 | 7
 // 70-80 | 8
 // 80-90 | 9
void loop() {
    // ============   Read Ultrasonic sensor ==============
    long duration, inches, cm;
    pinMode(trigPin, OUTPUT);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    cm = duration / 29 / 2;
    Serial.println(cm);

     
    // ============   Draw digits ==============
    //Draw digit 1
    leds[2] = CRGB::Red;
    leds[5] = CRGB::Red;
    leds[6] = CRGB::Red;
    leds[10] = CRGB::Red;
    leds[13] = CRGB::Red;      
    FastLED.show();  
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
    //Draw digit 2
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[6] = CRGB::Red;
    leds[9] = CRGB::Red;
    leds[13] = CRGB::Red;      
    leds[14] = CRGB::Red;  
    leds[15] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
    //Draw digit 3
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[6] = CRGB::Red;
    leds[10] = CRGB::Red;      
    leds[14] = CRGB::Red;  
    leds[15] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
    //Draw digit 4
    leds[0] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[7] = CRGB::Red;
    leds[6] = CRGB::Red;
    leds[5] = CRGB::Red;      
    leds[10] = CRGB::Red;  
    leds[13] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
 
    //Draw digit 5
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[7] = CRGB::Red;
    leds[8] = CRGB::Red;      
    leds[9] = CRGB::Red;  
    leds[10] = CRGB::Red;  
    leds[13] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
 
    //Draw digit 6
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[7] = CRGB::Red;
    leds[8] = CRGB::Red;      
    leds[9] = CRGB::Red;  
    leds[10] = CRGB::Red;  
    leds[13] = CRGB::Red;  
    leds[15] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
 
    //Draw digit 7
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[5] = CRGB::Red;
    leds[9] = CRGB::Red;      
    leds[15] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
 
    //Draw digit 8
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[5] = CRGB::Red;
    leds[7] = CRGB::Red;      
    leds[8] = CRGB::Red;  
    leds[9] = CRGB::Red;  
    leds[10] = CRGB::Red;  
    leds[13] = CRGB::Red;  
    leds[15] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();
 
 
    //Draw digit 9
    leds[0] = CRGB::Red;
    leds[1] = CRGB::Red;
    leds[2] = CRGB::Red;
    leds[5] = CRGB::Red;
    leds[7] = CRGB::Red;      
    leds[8] = CRGB::Red;  
    leds[9] = CRGB::Red;  
    leds[10] = CRGB::Red;  
    leds[13] = CRGB::Red;  
    FastLED.show();    
    delay(1000);
    //Turn off screen
    for (int i=0 ; i<16 ; i++ )
      leds[i] = CRGB::Black;
    FastLED.show();

}
 

בונוס: פתרון המשימה

פתרון המשימה:

  
  //FastLED
  #include <FastLED.h>
  #define NUM_LEDS 16
  #define DATA_PIN 33
  CRGB leds[NUM_LEDS];

  //Ultrasonic Setup
  int trigPin = 12;    // Trigger
  int echoPin = 35;    // Echo
 
void setup() {
  //FastLED setup
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
  Serial.begin(9600);
}

     // 0-10 | 1
     // 10-20 | 2
     // 20-30 | 3
     // 30-40 | 4
     // 40-50 | 5
     // 50-60 | 6
     // 60-70 | 7
     // 70-80 | 8
     // 80-90 | 9
     
void loop() {
    // ============   Read Ultrasonic sensor ==============
    long duration, inches, cm;
    pinMode(trigPin, OUTPUT);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    cm = duration / 29 / 2;
    Serial.println(cm);

     
    // ============   Draw digits ==============
    if ((cm>0)&&(cm<10))
    {
      //Draw digit 1
      leds[2] = CRGB::Red;
      leds[5] = CRGB::Red;
      leds[6] = CRGB::Red;
      leds[10] = CRGB::Red;
      leds[13] = CRGB::Red;      
      FastLED.show();  
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }
    if ((cm>10)&&(cm<20))
    {
      //Draw digit 2
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[6] = CRGB::Red;
      leds[9] = CRGB::Red;
      leds[13] = CRGB::Red;      
      leds[14] = CRGB::Red;  
      leds[15] = CRGB::Red;  
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }
    if ((cm>20)&&(cm<30))
    {
      //Draw digit 3
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[6] = CRGB::Red;
      leds[10] = CRGB::Red;      
      leds[14] = CRGB::Red;  
      leds[15] = CRGB::Red;  
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }
    if ((cm>30)&&(cm<40))
    {
      //Draw digit 4
      leds[0] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[7] = CRGB::Red;
      leds[6] = CRGB::Red;
      leds[5] = CRGB::Red;      
      leds[10] = CRGB::Red;  
      leds[13] = CRGB::Red;  
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }
    if ((cm>40)&&(cm<50))
    {
      //Draw digit 5
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[7] = CRGB::Red;
      leds[8] = CRGB::Red;      
      leds[9] = CRGB::Red;  
      leds[10] = CRGB::Red;  
      leds[13] = CRGB::Red;  
   
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }

    if ((cm>50)&&(cm<60))
    {
      //Draw digit 6
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[7] = CRGB::Red;
      leds[8] = CRGB::Red;      
      leds[9] = CRGB::Red;  
      leds[10] = CRGB::Red;  
      leds[13] = CRGB::Red;  
      leds[15] = CRGB::Red;  
   
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }

    if ((cm>60)&&(cm<70))
    {
      //Draw digit 7
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[5] = CRGB::Red;
      leds[9] = CRGB::Red;      
      leds[15] = CRGB::Red;  
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }

    if ((cm>70)&&(cm<80))
    {
      //Draw digit 8
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[5] = CRGB::Red;
      leds[7] = CRGB::Red;      
      leds[8] = CRGB::Red;  
      leds[9] = CRGB::Red;  
      leds[10] = CRGB::Red;  
      leds[13] = CRGB::Red;  
      leds[15] = CRGB::Red;  
   
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }

    if ((cm>80)&&(cm<90))
    {
      //Draw digit 9
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[5] = CRGB::Red;
      leds[7] = CRGB::Red;      
      leds[8] = CRGB::Red;  
      leds[9] = CRGB::Red;  
      leds[10] = CRGB::Red;  
      leds[13] = CRGB::Red;  
      FastLED.show();    
      delay(1000);
      //Turn off screen
      for (int i=0 ; i<16 ; i++ )
        leds[i] = CRGB::Black;
      FastLED.show();
    }
}