I can't understand this and its driving me crazy. Please help me

//Automatic Nightlight

const int LED=9;
const int LIGHT=0;
const int MIN_LIGHT=200;
const int MAX_LIGHT=900;
int val = 0
void setup() {
  pinMode(LED,OUTPUT);
}

void loop(){
  
  
  val = analogRead(LIGHT);
  val = map(val,MIN_LIGHT, MAX_LIGHT, 255,0);
  val = constrain(val,0,255);
  analogWrite(LED,val);
  
}

I am not Kreskin, what exactly should it do, and what does it do?

OMG, I don't understand it, either!

What uC are you using? Just as an example using an Arduino Uno Rev3 you can't use pin 1 oe pin 0 as an analog input which brings us to what light sensor is being used? How is it configured? A schematic would be nice. If you are measuring an analog value from your sensor you need to use an analog input.

Ron

1 Like

Can you post a schematic? Are you using an LDR? Did you mean to use A0 rather than 0 for LIGHT? Have you tried printing out val after reading the value here?

val = analogRead(LIGHT);
1 Like
const int LIGHT=0;

did you mean ?

const int LIGHT=A0;

Are you using a light dependent resistor to measure light in the room? Did you use an LDR module, an LDR configured as a voltage divider or just an LDR?

Where did these preset minMax limits come from? Do they actually represent the raw val = analogRead(LIGHT); readings you empirically noted during LDR circuit testing and calibration?

1 Like

Except

When you use the analogRead() function, "0" and "A0" refer to the same pin, the analog pin 0. These two invocations of analogRead() are equivalent: analogRead(0);

a7

3 Likes

@Delta_G @alto777 I didn't know analogRead() would interpret 0 as A0. Thanks for informing me about that. Now it all makes sense :slight_smile:

1 Like

Thanks a bunch, I also was not aware of that.

Ron

.

I understand this and if you ask a question I try to help you.

missing a ;
try

int val = 0;

The author didn't ask a single question, didn't even visit the thread once - why are you writing all this to him?

2 Likes

Sorry, please tell us the new rules of engagement. He may not be hovering over his keyboard, waiting with bated breath for pronouncements from the gods.
Me, I just saw the problem, so I identified it.
Apologies. I'll leave again.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.