Is this correct?

Unsure if i should have post in “Project Guidance” or “Programming Guidance”
I use a simulator until my UNO arrives zzzz, the simulator seems to be working ok,
but it doesn’t look correct to me.

#include <avr/sleep.h>

const int ldr=A2;				//LDR Sensor
int tempPinA=A0;                             	//input read pin for LM35_A is Analog Pin 0
int tempPinB=A1;                             	//input read pin for LM35_B is Analog Pin 1
int outPin=13;                               	//output pin Pin13
float valA=0;                                	//variable_A to store the value from sensor_A
float valB=0;                                	//variable_B to store the value from sensor_B

void setup() {

  if(analogRead(ldr) <= 10)			//At Dusk 
  { 
    set_sleep_mode(SLEEP_MODE_PWR_DOWN); 	//Set Sleep Mode
    sleep_enable(); 				//Activate
    sleep_mode(); 				//ZzZzZzZzZzZz
    //Dreaming of a Brighter Future
  }
  pinMode(outPin, OUTPUT);                    	//outPin set to output
}
void loop ()                                 	//loop
{
  valA = analogRead(tempPinA);                	//read the value of sensor_A
  valB = analogRead(tempPinB);                	//read the value of sensor_B
  if (valA > valB)                            	//Check if temperature_A is greater than temperature_B
  {
    digitalWrite (outPin, HIGH);              	//If temperature_A greater than temperature_B set outPin to ON
  }
  else                                        	//Otherwise
  {
    digitalWrite (outPin, LOW);               	//If temperature_A is equal or lesser set outPin to OFF
  }
  delay(900000);                              	//wait 15 minutes
}

Any guidance or books to learning where to write smaller code very much appreciated.

to learning where to write smaller code very much appreciated

That code is about as small as you get without being over complex and silly.

However:- delay(900000 ) ; is not a good idea, look at the blink without delay example to allow you to do other things during this long time.

Hi, A list of books you might want is here: http://arduino-info.wikispaces.com/Arduino-GoodBooks

I recommend: Programming Arduino "Getting started with Sketches"- Simon Monk - Excellent start to programming

The loop() function:

void loop ()                                    //loop
{
  valA = analogRead(tempPinA);                 //read the value of sensor_A
  valB = analogRead(tempPinB);                 //read the value of sensor_B
  if (valA > valB)                               //Check if temperature_A is greater than temperature_B
  {
    digitalWrite (outPin, HIGH);               //If temperature_A greater than temperature_B set outPin to ON
  }
  else                                         //Otherwise
  {
    digitalWrite (outPin, LOW);                //If temperature_A is equal or lesser set outPin to OFF
  }
  delay(900000);                                //wait 15 minutes
}

Could be written more simply as:

void loop () {
    digitalWrite(outPin, analogRead(tempPinA) > analogRead(tempPinB));
    delay(900000UL);
}

Your code has a bug in that delay() call: it won't really wait 15 minutes. The delay() function expects an unsigned long argument, but by default in C any integer constant is taken as "int" unless otherwise specified. Your 9000000 is truncated to fit in 16 bits, so you'll wait quite a bit less than 15 minutes. You'll see I put UL at the end of 9000000, to make it an unsigned long: 9000000UL.

But, as Grumpy Mike has said, when you want this sketch to do anything more the delay() pattern breaks and you need to graduate to Blink Without Delay.

Hope that's helpful...

-br

I am very appreciative of the responses :) Thankyou

@Mike I may be mistaken, but will that consume more power?

@billroy

void loop () {
    digitalWrite(outPin, analogRead(tempPinA) > analogRead(tempPinB));
    delay(900000UL);
}

I can not see how the state changes from HIGH to LOW, UL must read more o.O

@terryking228 Cheers!

Think about the logic in the boolean expression for a minute.

If "analogRead(pinTempA) > analogRead(pinTempB)" is false, the pin is set LOW.

-br

:fearful:

Tunnel Vision, Sorry, I believe it's correct because of your status, but i do not understand fully, why it is correct.

I do not see, how digitalwrite, set the pin low/high~~t~~ because it is checking if value 1 is > value 2.

My bad.

I do not see, how digitalwrite, set the pin low/hight because it is checking if value 1 is > value 2.

(x > y) is a boolean expression - it is either true or false. true happens to have the same value as HIGH, and false the same value as LOW.

In C/C++, the convention is that ANY expression that evaluates to zero is false, and any non-zero value evaluates to true.

I was blind, but now i see.

Digitalwrite is dependant on the expression A > B, if A > B = True, Then write True, High or On. if A > B = False, Then write False, Low or Off.

My brain is a sponge, and this community is the water, pity my sponge dries up so quick lol

just beat me lol

JB_AU: Tunnel Vision, Sorry, I believe it's correct because of your status, but i do not understand fully, why it is correct.

I agree with Reply #1. The change being suggested results in code that is shorter but more complex (harder to understand). I recommend that you stick with the original code since simplicity is more important than compactness in this situation.

@PeterH , while i was writring my reply AWOL had already posted, i had already figured it out, but thanks tho.

Since this removes the need for valA and valB, are the values seen as whole or decimal values ?

Since this removes the need for valA and valB, are the values seen as whole or decimal values ?

analogRead returns integer results.

eek! 1v/1024 = 0.0009765625

So inside the MCU this is 0 ?

1v/1024 = 0.0009765625

Whilst I can't argue with the arithmetic, I don't see the relevance, and I will argue that you forgot the units of the result.. Where did 1 volt come from?

I understand, 0.0009765625 = 1, 0.001953125 = 2 etc

I had understood that 1v the max from the sensor / 1024 = 0.0009765625 , and this decimal number is what is retained, i did not fully comprehend that this value is = 1 from a range of 1024 units, 0 to 1023.

I had understood that 1v the max from the sensor / 1024 = 0.0009765625

Are you saying that your sensor only provides 1 volt maximum (first mention of this)? In which case, assuming you're using a 5V system with the default 5V reference, then 1V will give you an analogue reading of around 1/ 5 * 1024 = 204.

haha MCU are so much fun :zipper_mouth_face:

That is ok! regardless to this project, it still works.

But why is it multiplied by 1024, and not divided?

The analogue-to-digital converter compares an input voltage against successive halvings of the interval between zero volts and the analogue reference voltage, so there are 1024 steps between zero volts and five volts (by default). One step therefore represents 5 / 1023 volts = 4.88mV

Is there a precision expected?

I see, regardless the ref is 5v.

So the lowest value after 0v is 0.2......204

OMG Tuna! Its not a Tuna!