While, Do While, For (Skematik, Listing, FlowChart)



While, Do While, For
Oleh : Dani Hermawan


1.    “WHILE”
 
a.     Skematik :


b.    Listing Program :

      // These constans won’t change :
      int sensorPin= A5;// membuat variabel LDR untuk pin A5
      int ledPin= 0;// membuat variabel LED untuk pin 0
      int indicatorLedPin= 13; //pin that built-in LED is attached to
      int buttonPin= 2// pin that the button is attached to

     //These Variables will Change :
      int sensorMin= 1023; // Minimum sensor Value
      int sensorMax= 0;//Maximum sensor value
      int sensorValue= 0;//The Sensor Value

      void setup()
      // set the led pin as outputs and the switch pin as input :
      {
      pinMode(indicatorLedPin, OUTPUT);
     
      pinMode(A5, INPUT);// menentukan LDR menjadi OUTPUT
      pinMode(0, OUTPUT);// menentukan LED menjadi OUTPUT
      Serial.begin(9600);// komunikasi Arduino ke Komputer
      }

     void loop()
     //While the button is pressed, take calibration readings :
     while(digitalRead(sensorPin)==HIGH)
    {
    calibrate;
    }
    // signal the end of the calibration period :
    digitalWrite(indicatorLedPin, LOW);
    // read the sensor
    sensorValue= analogRead(sensorPin);
    // apply the calibration the sensor reading :
    sensorValue=map(sensorValue,sensorMin,sensorMax,0,255);
    //in case the sensor value is outside the range seen during calibration
    sensorValue=constrain(sensorValue,0,255);
    //fade the LED using the calibrated value:
    analogWrite(ledPin,sensorValue);
    }

    void calibrate(){
  // turn on the indicator LED to indicate that calibration is happening:
  digitalWrite(indicatorLedPin,HIGH);
  // read the sensor:
  sensorValue=analogRead(sensorPin);
  // record the maximum sensor value
  if(sensorValue>sensorMax){
  sensorMax=sensorValue;
  }

  // record the minimum sensor value
  if(sensorValue<sensorMin){
  sensorMin=sensorValue;
  }
  }

c.      FlowChart “While”




















2.    “DO WHILE”

a.     Skematik :















b.    Listing Program :

int x=4; int y=1;
void setup() {
pinMode (4,OUTPUT); pinMode (7,OUTPUT);
pinMode (5,OUTPUT); pinMode (8,OUTPUT);
pinMode (6,OUTPUT); pinMode (9,OUTPUT);
}
void loop()
 {
do
{
digitalWrite (x,y);
x=x+1;
delay(100);
} while (x <=9 );
if (x>9) {
x=4;
if (y==1) y=0;
else y=1;
}
}

c.      Flow Chart “Do While”

 
















 

3.    “FOR”

a.     Skematik :
















b.    Listing Program :
int timer =100;           // The higher the number, the slower the timing.

voidsetup(){
  // use a for loop to initialize each pin as an output:
  for(intthisPin=2;thisPin<8;thisPin++){
    pinMode(thisPin,OUTPUT);
  }
  }

voidloop(){
  // loop from the lowest pin to the highest:
  for(intthisPin=2;thisPin<8;thisPin++){
    // turn the pin on:
    digitalWrite(thisPin,HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin,LOW);
  }


  // loop from the highest pin to the lowest:
  for(intthisPin=7;thisPin>=2;thisPin--){
    // turn the pin on:
    digitalWrite(thisPin,HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin,LOW);

  }
  }

c.      FlowChart “For”


   


















Butuh materi Elektronika lainnya ? klik disini.
 

Komentar

Postingan populer dari blog ini