Stap 3: De schets
Kopieer de onderstaande code in het venster van de Arduino IDE.
/* littleBits Arduino Module Marty Vance 20 June 2014 Slider_Blink What is supposed to happen: A slide dimmer on pin A0 changes the blink rate of an RGB LED on pin 5. This sketch includs the following commands: pinMode( pin, mode ); // pin = a digital pin, mode = OUTPUT or INPUT digitalWrite( pin, value ); // pin = a digital pin that has been set to OUTPUT, value = HIGH or LOW delay( time ); // time = time of delay in milliseconds */ /* Variable initialization In most sketches, you'll want to initialize some variables before anything else happens. This way they are already available when setup() runs. We create variables for the pins we'll be using and a few more that will be needed as the sketch runs. */ uint8_t led_pin = 5; // pin for the LED uint8_t sldr_pin = A0; // pin for the slide dimmer int sldr_val = 512; // the blink rate initial value int blink_val = 0; // the blink duration int t = 0; // used to store the current time during each execution of loop() int l = 0; // used to store the time of the previous loop() boolean o = false; // whether to turn the LED on or off int min_rate = 50; // fastest blink rate in milliseconds int max_rate = 1000; // slowest blink rate in milliseconds /* setup - This is where we initialize the sketch Setup includes commands that set the stage for the rest of the sketch. The commands inside of setup are only executed once. After all the commands in setup have been executed, the sketch moves to loop. */ void setup(){ // this is the beginning of setup // Since we are going to use pin 5 to control an output module // we need to set the pinMode of pin 5 to OUTPUT, // this means voltage will flow out of the pin. pinMode( led_pin, OUTPUT ); // set pin 5 to OUTPUT mode pinMode( sldr_pin, INPUT ); // set pin A0 to INPUT mode sldr_val = analogRead(sldr_pin); // read the slide dimmer right away } // this is the end of setup /* loop - This is the main part of every arduino sketch. Loop is a series of commands that executed one after another in order of top to bottom. When all the commands have been executed, loop begins again from the top. This goes on for as long as there is power running the Arduino. */ void loop(){ // This is the beginning of loop, each command below is executed in order of top to bottom t = millis(); // get the current time /* This loop runs without calling delay(): it continuously reads the slider value, but only changes the blink rate when enough time has passed, which is determined by the if statement. See the Blink Without Delay example sketch. */ sldr_val = analogRead(sldr_pin); if (t - l >= blink_val) { Serial.print("Slider value: "); Serial.print(sldr_val); blink_val = map(sldr_val, 0, 700, min_rate, max_rate); // convert the possible range of sldr_val to within the bounds of the blink rates Serial.print(" => Blink interval: "); Serial.println(blink_val); // watch the serial monitor to see exactly what map() is doing digitalWrite(led_pin, (o ? HIGH : LOW)); // Turn the LED on or off based on whether o is true or false o = !o; // if o is true, make it false; if o is false, make it true l = t; // this loop's time is the previous time for the next loop } } // this is the end of loop, now return to the beginning of loop and execute the commands again
Klik op de Upload-knop in de Arduino IDE. Als alles klopt, na een paar seconden u de gele LEDs op de Arduino module in de buurt van de USB-connector knipperen snel voor een paar seconden ziet, moet dan uw RGB LED beginnen te knipperen.
Beweeg de schuif-Dimmer om te veranderen hoe snel de LED knippert!