Program dibuat sesuai software aplikasi arduino
Program Arduino |
Baca juga artikel sebelumnya --- Rangkaian Dimmer
1. Proram Dimmer yang dibuat dengan input manual pada serial monitor
//============================
int ledPin = 11;
void setup()
{
Serial.begin(9600);
Serial.println("Serial connection started, waiting for instructions...n0 = Off n1 = 25% n2 =50% n3 = 75% n4 = 100%");
}
void loop ()
{
if (Serial.available()) {
char ser = Serial.read(); //read serial as a character
//NOTE because the serial is read as “char” and not “int”, the read value must be compared to character numbers
//hence the quotes around the numbers in the case statement
switch (ser)
{
case '0':
analogWrite (ledPin, 0);
break;
case '1':
analogWrite(ledPin, 25); //input angka "1" dimasukkan melalui serial monitor software arduino
break;
case '2':
analogWrite(ledPin, 50); //angka "50" merupakan nilai yang berpengaruh pada intensitas lampu
break;
case '3':
analogWrite(ledPin, 120);
break;
case '4':
analogWrite(ledPin, 175);
break;
case '5':
analogWrite(ledPin, 245);
break;
default:
Serial.println ("Invalid entry");
}
}
}
2. Proram Dimmer yang dibuat dengan input LDR atau Photo Dioda
//============================
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);
Serial.println();
Serial.print("led reading = ");
Serial.println(LEDbrightness);
delay(100);
}
0 comments:
Post a Comment