Need Help Coding!

I need to figure out how to code this piece of code so that it will turn on when it is dark and off when it is light. I was going to use a photoresistor but I don't know what code to use if I want if to turn on in the dark and off in the light. Here is the code that I have so far. I need someone to maybe code this so that this whole thing will turn on in the dark and off when it is light.
const int hot = 87; //set hot parameter
const int cold = 75; //set cold parameter
void setup() { pinMode(A2, INPUT); //sensor pinMode(2, OUTPUT); //bluepinMode(4, OUTPUT); //red Serial.begin(9600); } void loop() { int sensor = analogRead(A2); float voltage = (sensor / 1024.0) * 5.0; float tempC = (voltage - .5) * 100; float tempF = (tempC * 1.8) + 32; Serial.print("temp: "); Serial.print(tempF); if (tempF < cold) { //cold digitalWrite(2, HIGH);digitalWrite(4, LOW); Serial.println(" It's Cold."); } else if (tempF >= hot) { //hot digitalWrite(2, LOW);digitalWrite(4, HIGH); Serial.println(" It's Hot."); }delay(10); }

You understand that code runs once in setup() and runs many times in loop()?

You read stuff like; Topic: How to get the best out of this forum (short version) and Topic: Read this before posting a programming question ... but still declined to put your properly formatted code in code tags, why?

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

www.google.de/search?as_q=arduino++photoresistor+demo+code

best regards Stefan

Here is the code that I have so far.

Does it really look like that in the iDE ?

lionchipmunkcoder11:
I need to figure out how to code this piece of code so that it will turn on when it is dark and off when it is light.

1. This is your hardware setup shown in Fig-1.
therm.png
Figure-1: Hardware setup of Thermistor circuit.

2. This is your sketch made out of codes you have posted.

const int hot = 87; //set hot parameter
const int cold = 75; //set cold parameter
void setup() 
{ 
    pinMode(A2, INPUT); //sensor 
    pinMode(2, OUTPUT); //blue
    pinMode(4, OUTPUT); //red 
    Serial.begin(9600); 
} 

void loop() 
{ 
    int sensor = analogRead(A2); 
    float voltage = (sensor / 1024.0) * 5.0; 
    float tempC = (voltage - .5) * 100; 
    float tempF = (tempC * 1.8) + 32; 
    Serial.print("temp: "); 
    Serial.print(tempF); 
    if (tempF < cold) 
    { //cold 
       digitalWrite(2, HIGH);
       digitalWrite(4, LOW); 
       Serial.println(" It's Cold."); 
    } 
    else 
       if (tempF >= hot) 
       { //hot 
           digitalWrite(2, LOW);
           digitalWrite(4, HIGH); 
           Serial.println(" It's Hot."); 
        }
     delay(10); 
}

Can you please, post the type number of your LDR or post a web link to it? I wish to know its responses (resistance values vs temperatures). At bright light, the resistance is about 500 ohms; at darkness, the resistance is about 200 k.

therm.png

GolamMostafa:
Figure-1: Hardware setup of Thermistor circuit.

Hot?
Cold?
Light?
Dark?
LDR?
Thermistor?
tempC?

What is going on here?

TheMemberFormerlyKnownAsAWOL:
Hot?
Cold?
Light?
Dark?
LDR?
Thermistor?
tempC?

What is going on here?

I have just re-arranged OP's code; however, I missed the hot and cold parameters which I have entered now in the sketch.

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