Arduino Advent Calendar Day 03 - Light Dependent Resistor


#1

Need help for the Arduino Advent Calendar Day 3 Guide? Just post on this thread and our highly trained team of Arduino ninjas will be able to help you in no time!


#2

Hi Little Bird

the IDE was for temperature rather than light.
In the end I found this

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
delay(1000);
}

I found it odd that the readings were higher the darker the room was and couldn’t work out how to reverse the readings…

Is there a better IDE
Regards ZoeM


#3

Thanks for the heads up, I accidentally overwrote it with the day 4 code! I have fixed it up :slight_smile:
The correct code is:

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

Cheers,

Marcus


#4

Hi Marcus, thanks for this.
I have set up my board with 2 LEDs as per day 2
I would like to get them to each do a different thing, ie flicker blink and/or fade?

How do I connect 2 of the programs from day 2?

Thanks
ZoeM


#5

If you’re asking how to merge the two programs on day 2; the first steps are;

  • marking out the scopes in the two programs; global (everything before setup), the setup function (everything inside setup), and the loop function (everything inside loop),
  • making an empty program to work in,
  • combining the global sections by copy and paste,
  • combining the setup functions into one setup function,
  • the same with the loop function,

Then figure out what resources, such as pins, need to be shared between the two.

After that, you have to figure out how to deal with the different delays.

I’ve just tried this, but not tested it, and I hope it would look something like this;

int brightness = 0;
int fadeAmount = 5;

void setup() {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop() {
  analogWrite(9, brightness);

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }

  digitalWrite(10, HIGH);
  delay(100);
  digitalWrite(10, LOW); 
  delay(100);
}

The next step is to tune the delays and the fade amount to work together.