///////////////////////////////////// liaison ceramic /////////////////////////////////////// // the following code makes talk the LDR (light dependent resistor) to the led // hier all the vavriable are declared // variables for the sensor circuit int analogPin=1; // sets the analog pin to communicate to the LDR int val; // stores the read values // variable for the led circuit int ledpin = 10; // sets the pin 10 to communicate to the led int key; // stores the analog data int key1; // stores the analog data after from 'key' int i = 0; // store the values to meke the led blink int j = 0; // store the values to make the led turn on int k= 255; long previousMillis = 0; // stores last time LED was updated long interval = 40; boolean onState = false; // asks if the LDR state is on // this function will run just once, hier all the general setup are written void setup (){ Serial.begin(9600); // opens serial port, sets data rate to 9600 bps Serial.println ('b'); // prints the data to the serial port pinMode(ledpin, OUTPUT); // configures the pin 10 to behave as an output } // this one runs consecutively void loop(){ val = analogRead(analogPin); // 'val' stores the light data coming from analog pin if (val>900){ // if the light intensity is under 900 if (onState == false){ // is the LDR state is off Serial.print('b'); // the letter 'b' is wirtten to the console onState= true; // and the LDR state will turn on delay(2000); // with a 2 seconds delay } } else { // otherwise if (onState == true){ // if the LDR state is off Serial.print('t'); // the letter 't' is printed to the console onState = false; // and the LDR state will turned on delay(2000); // with a 2 seconds delay } } if (Serial.available()){ // if the number of bytes are available for reading over the serial port key = Serial.read(); // the value 'key' stores the serial data when they come if (key == 't'){ // if the coming data are equal to 't', so the light is turned on fadeToOn(ledpin); // the led fades on through the function fadeToOn () key1=key; // and those values are stored into 'key1' } if (key == 'b'){ // if the coming data are equal to 'b', the light is tunres off turnOff(ledpin); // the led turns off through the turnOff () function key1=key; // and 'key1' strors the data } } } ////////////// function: fade to on /////////////// void fadeToOn(int ledpin){ // the led connected to pin 10 fades on through this function while(j<255){ // while the value is less than 255 if ((millis() - previousMillis) > interval) { analogWrite(ledpin, j); // sets the value (range from 0 to 255) j+=5; // and increases to 5 previousMillis = millis(); // the variable previousMillis stores the values of the millis () function } } if (j=255){ // if it is equal to 255 analogWrite(ledpin,255); // the ledpin will sets to 255 } j=0; // and the 'j' will empty } ////////////// function: blink with fade /////////////// void blink(int ledpin){ // sets the led pin 10 to be used to make the led blink while(i<25){ // while the variable 'i' is less than 25 if ((millis() - previousMillis) > interval) { analogWrite(ledpin, i); // sets the value (range from 0 to 255) i+=5; // the value increases of 5 previousMillis = millis(); // the variable previousMillis stores the values of the millis () function } } while(i>=25 && i<255){ // while the value is greater than 25 and less than 255 if ((millis() - previousMillis) > interval) { analogWrite(ledpin, i); // sets the value (range from 0 to 255) i+=5; // the value increases of 5 previousMillis = millis(); // the variable previousMillis stores the values of the millis () function } } i=25; // assigns value 25 while(k>25){ // while the variable 'k' is greater than 25 if ((millis() - previousMillis) > interval) { analogWrite(ledpin, k); // sets the value (range from 0 to 255) k-=5; // deincreases of 5 previousMillis = millis(); // the variable previousMillis stores the values of the millis () function } } k=255; // assigns 255 to 'k' } ////////////// function: turn off the led /////////////// void turnOff(int ledpin){ analogWrite (ledpin, 0); }