analogRead Basic Help.

Hello and thanks for the time!

I'm veary new to this coding and I am sorry if this is allready to be found some where. But I try google it and look on youtube but do not understand it.
digitalWrite and Read are easy to work with. But I have problems useing sensors and analog
I still do not get it how my analogRead can turn things on and off.


Example.

int Sensor1 = A0;
int Relay = 8;

void setup() {
pinMode(Sensor1, INPUT);
pinMode(Relay, OUTPUT);
}

void loop() {

// How will the code look if I want the Relay to turn on if Sensor1 have low value? and else the Relay is off.

}

If some one can give me some exmples on values I be really thankfull.

You read an analogue pin.
2)
Compare the value that you have read against a value that you determined using an if statement. The range is from 0 to 1023; e.g. if(value < 512)
3)
Take action based on the result of the compare (in the same way you would take action after a digitalRead)

I still do not get it how my analogRead can turn things on and off.

It can't. analogRead does not turn pins on or off.

You can use analogRead() to get a value, and an if statement to compare that value against a threshold, and then use digitalWrite() in the body of the if statement, or the body of the else statement, to turn the pin on or off.

I done some diffrent things in my code but I think i fall off, when i start adding diffrent new statements.

int sensorValue1 = analogRead(Sensor1);

if sensorValue1 // at this point I start loosing it :smiley:

The basis of if statements is that you test to see IF something is equal/bigger/smaller/other condition than something else. If it is you do something. And maybe you want to add an "Or if it isn't do something different" part which is the "else" part of if/else.

So it looks something like:

if (sensorValue1 > 200)  // or sensorValue1 <= sensorValue2 or all sorts of other comparisons
{
Do something like maybe switch something on
}
else
{
do something else like maybe switch the thing off
}

Which part of that is causing you trouble?

Steve

slipstick:
The basis of if statements is that you test to see IF something is equal/bigger/smaller/other condition than something else. If it is you do something. And maybe you want to add an "Or if it isn't do something different" part which is the "else" part of if/else.

So it looks something like:

if (sensorValue1 > 200)  // or sensorValue1 <= sensorValue2 or all sorts of other comparisons

{
Do something like maybe switch something on
}
else
{
do something else like maybe switch the thing off
}




Which part of that is causing you trouble? 

Steve

Ok I know what i did wrong. The else/or Was wrong. I manage to turn it on but it would never go off.
I added this and now it works.
if (sensorValue > 400) digitalWrite(Relay, HIGH);
if (sensorValue < 500) digitalWrite(Relay, LOW);

Maybe not the correct way to do it, but at last now it turns on and off

What happens when the reading is between 400 and 500?

if (sensorValue >500) digitalWrite(Relay, HIGH);
if (sensorValue < 499) digitalWrite(Relay, LOW);

x < y (x is less than y)
x > y (x is greater than y)

Would be more correctly then :slight_smile:

Was just happy to get it to click. did not pay much attention to the value right now :smiley:

What's wrong with the even simpler version?

  if (sensorValue >500) digitalWrite(Relay, HIGH);
  else digitalWrite(Relay, LOW);

Steve

What's wrong with the even simpler version?

It has no hysteresis.