Complete Newbie question

Hi

I am trying to programme some code on a lilypad circuit

I am trying to get the led to turn on when the light drops around a certain level

So far i have the following

int ledPin = 13;	
int sensorPin = 0;	
int sensorValue ;	
 	 
void setup()	 
{	 
         pinMode(ledPin, OUTPUT);	
         Serial.begin(9600);	
         digitalWrite(ledPin, HIGH);	
}	 
 	 
void loop()	
{
if (sensorvalue <= 55)

 digitalWrite(ledPin, HIGH); 
}

Obviously this is just a test with the led on the lilypad but im a bit stuck - any help would be grateful.

Also - if i want to use multiple leds - do i have to amed the following line to include all the pins i am using?

int ledPin = 13;

You haven't got anything to read "sensorvalue" Maybe an "analogRead"?

Ok

I have got a sensor attached to a0 and to the - and +

its reading about 55 in the current light - which is dusk

where would i put “analogRead”?

int ledPin = 13;	// LED is connected to digital pin 13
int sensorPin = 0;	// light sensor is connected to analog pin 0
int sensorValue ;	// variable to store the value coming from the sensor

void setup()	 
{	 
  pinMode(ledPin, OUTPUT);	// sets the ledPin to be an output
  Serial.begin(9600);	//initialize the serial port
  digitalWrite(ledPin, LOW);	// turn the LED on
}	 

void loop()	// run over and over again
{
  if 
    analogRead(analogPin);    // read the input pin
  int(sensorvalue <= 55) // if sensor level goes below 55 

    digitalWrite(ledPin, HIGH); // turn led on
}

Apologies for the questions? Its for a uni end of year show and dont have long left!

void loop() {
if ( analogRead(analogPin) <= 55) {

digitalWrite(ledPin, HIGH); // turn led on
}
}

Of course, there’s nothing to turn the LED off again

I could turn it off with this couldnt i?

void loop() {
if ( analogRead(analogPin) <= 55) // read sensor
{

digitalWrite(ledPin, HIGH); // turn led on
}
{
if ( analogRead(analogPin) => 55) // read sensor
{

digitalWrite(ledPin, LOW); // turn led off
}

But what if it reads 55?

Maybe just use an "else".

Ok

So my code now looks like this

int ledPin = 13;	// LED is connected to digital pin 13
int sensorPin = 0;	// light sensor is connected to analog pin 0
int sensorValue ;	// variable to store the value coming from the sensor

void setup()	 
{	 
  pinMode(ledPin, OUTPUT);	// sets the ledPin to be an output
  Serial.begin(9600);	//initialize the serial port

}	 

void loop()   {
  if analogRead(sensorPin); <= 55    // read sensor
 {

    digitalWrite(ledPin, HIGH); // turn led on
}
 {
  if  analogRead(sensorPin); => 54   // read sensor
  }
 {
   digitalWrite(ledPin, LOW); // turn led off
   }

}

But its saying the following errors

Blink:26: error: expected primary-expression before ‘=’ token
Blink:26: error: expected primary-expression before ‘>’ token
Blink:27: error: expected `;’ before ‘}’ token

 if analogRead(sensorPin); <= 55

I don’t think I wrote my example code like that.

If something isn’t “>=”, it is normally “<”.
That’s what “else” is for.

Im sorry but i am confused?

When i use the coding example you gave

void loop()   {
  if ( analogRead(analogPin) <= 55)    // read sensor
 {

    digitalWrite(ledPin, HIGH); // turn led on
}

it says ‘analogPin’ was not declared in this scope and then the following errors

Blink:25: error: expected primary-expression before ‘>’ token
Blink:26: error: expected primary-expression before ‘}’ token
Blink:26: error: expected `;’ before ‘}’ token

How do i use else?

im struggling to understand this - i have tried searching all over for an answer so thanks for your patience and time

Im wanting to have the lights on when it gets darker (and im trying to figure out how to make the light “twinkle” as it will have fibre optic cables attached to the led)

There’s a really useful learning resource:
http://arduino.cc/en/Reference/Else

Thanks that really cleared up the ‘else’ issue

It now does what i want in regards to turning on and off - now i need to worry about making the led flash but im working with the blink programme so we will see what i come up with.

Edit - this code seems to work

int ledPin = 13;	// LED is connected to digital pin 13
int sensorPin = 0;	// light sensor is connected to analog pin 0
int sensorValue ;	// variable to store the value coming from the sensor

void setup()	 
{	 
  pinMode(ledPin, OUTPUT);	// sets the ledPin to be an output
  Serial.begin(9600);	//initialize the serial port

}	 
void loop()   {
  if ( analogRead(sensorPin) <= 100)    // read sensor
  
{

  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);              // wait for a second
}
else 
{

  digitalWrite(ledPin, LOW); // turn led off

}
}

Now - how do i now programme another 14 leds - i want them in groups of two per petal but i cant seem to get just one working on the code! Any help from anyone -