The code is as follows:
void loop(){
light = analogRead(5);
Serial.println(light);
analogWrite(3,light/4);
}
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.
Sign up to join this communityNot really a physics question, but I will answer anyway.
loop()
is the standard Arduino function which contains code that the microprocessor runs repeatedly (as opposed to code in setup()
which is only run once).
light = analogRead(5);
takes an analogue voltage reading from pin 5, encodes it as an integer value (either 10 or 12 bits depending on the Arduino model) and stores the result in the variable light
.
Serial.println(light);
outputs the value of the variable light
to the Arduino's serial port. This is a standard way of outputting data from the Arduino so that it can be displayed on a connected computer.
analogWrite(3, light/4);
outputs the value of light/4
as a phase-width modulated (PWM) signal to pin 3. Although Arduino output pins are either on (5 volts) or off (ground) at any point in time, PWM signals allow the Arduino to switch an output pin on and off rapidly so that the average voltage seen on that pin is an intermediate value. The duty cycle of the PWM signal which determines the average voltage on the pin is encoded as an 8 bit integer, so light
is probably divided by 4 here to convert it from 10 bits to 8 bits.
So the Arduino will repeatedly read an input voltage from pin 5, output an integer representing this voltage to the serial port, and create a PWM signal on pin 3 that has an average voltage the same as that of the input voltage.
See the Arduino language reference page for more information.