# Using Analogue input to trigger digital output

I am trying to compile a script to make an analogue input trigger an digital output. I am confused on how to implement the if command. I would like to have if value of A0 is great than 512 make digital out 4 go High.

Write a simple sketch showing how you think it would work, then attach it to this thread.
We will be able to point out where you are going wrong.

This way we can see how much you know.
.

You can use a program like the one I posted below.

``````void setup() {
pinMode(4, OUTPUT) //make sure you have set your pins as inputs or outputs
pinMode(A0, INPUT)
}

void loop() {
if(analogRead(A0) > 512) { //if the analog value of A0 is greater than 512 then
digitalWrite(4, HIGH); //write digital pin 4 high
}
else {
digitalWrite(4, LOW); //if it isn't higher than 512, make pin 4 low
}
}
``````

I guess we won't find out how much she/he knows.

LarryD:
Write a simple sketch showing how you think it would work, then attach it to this thread.
We will be able to point out where you are going wrong.

This way we can see how much you know.
.

He is right. Also, it is always a good idea to use the examples in the Arduino IDE to get you started and to understand simple things like if loops. Sorry LarryD tried posting this but I keep exceeding my number of posts in a 5 minute period.

This is what I have so far. Basically I am trying to have a trigger stay on for a second, every time the voltage goes past 1/2.

``````int output1 = 4;
int input1 = A0;
int sensorValue = 0;

void setup() {
pinMode(4, OUTPUT);
}

void loop () {
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
}
else {
digitalWrite(4, LOW);
}
``````

If the analog goes > 512 and stays there.
Do you want the output to toggle every 1 second,
or
do you want this toggle to just happen once?
.

It would like it toggle once, every time the voltage goes above the preset level.

Get rid of the ; at the end of the above line.

You need to watch your placement of { }
Try to place the correct number and repost your changes.

LarryD. I think I got ya. However now I am getting "error: 'else' without a previous 'if'"

``````int output1 = 4;
int input1 = A0;
int sensorValue = 0;

void setup() {
pinMode(4, OUTPUT);
}

void loop () {
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);

else
digitalWrite(4, LOW);
}
``````

Use CTRL T to format your code and check for matching { }

You have to watch your { } placements!

``````  if (analogRead(A0) > 512)
{
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
}
``````

Try again to see what happens.

Do you know what a "flag' is used for in programming?

LarryD. Thank You,

I know this is a fairly basic code, and I appreciate the help. I do not have my Arduino with me right now, so I can't perform a real life test.

However, one more question. Can I assume that if I add another analog input that controls another digital output in the same way. The following code should work? Or do I need to go back a separate the commands some more?

``````int output1 = 4;
int output2 = 5;
int input1 = A0;
int input2 = A1;
int sensorValue = 0;

void setup() {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}

void loop () {
{
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
}
else
digitalWrite(4, LOW);

{
digitalWrite(5, HIGH);
delay(1000);
digitalWrite(5, LOW);
}
else
digitalWrite(5, LOW);
}
``````

Make sure you look over your brackets. You are missing one to end the first else statement, and another at the very end to close the loop. Otherwise that code should work fine. Whenever you use delay(someValue) you freeze your Arduino for that time interval.
So writing your code with delays is never recommended.

If you have two or more things (even one thing) never, never, never use delay()

Always follow this format in programming, use matching { } on separate lines:

if(x == y)
{
// do some stuff
}
else
{
// do some stuff
}

.

Ok, this is becoming alot more complicated than I orginally planned. Great, I was hoping to be challenged. Im an analogue guy in a digital world.

Realizing now, that my original plan is not going to work as thought here is the scope of the project.

I have 6 triggers (pressure plates) located around a space. When someone steps on the plate, this pressure plate triggers a relay that makes some "Art" happen. I am using the ardunio just to convert the pressure plates to relay. The pressue plate should make the output go High for a period of 1 sec then go back Low. and stay low is nothing is happening.

I do not know about flags.

When an event (digital or analog) happens, you can set a variable to true or false.
We call this variable a Boolean type

boolean myFlag = false;
or
boolean enableFlag = true;

You can then use this in your sketch to enable a conditional if.

See if you can follow this:

``````int output1 = 4;
int input1 = A0;
int sensorValue = 0;
boolean flag = true; //when true allow for checking

void setup()
{
pinMode(4, OUTPUT);
} //END of setup()

void loop ()
{

if (flag == true && sensorValue > 512)
{
digitalWrite(output1, HIGH);
delay(1000);
// this freezes the Arduino for 1 second !!!
// we have to get rid of delay() to get to non blocking code but that's for later
digitalWrite(output1, LOW);
flag = false;
}

if(sensorValue <= 512)
{
//we are below 512 so we can now allow checking of a > 512 condition
flag = true;
}

} //END of loop()
``````

EDIT:
There is a technique called BlinkWithoutDelay BWD.
It allows you to do several things at once as you need in your application.
.

From what I gather. Using the Boolean logic is a more logical way of expressing the same idea.

It would be easier to add more and more variables and have them affect other variable. Say two sensors making relays go.

However, I am going to sign off for the night. I am going to go do research into writing code with boolean logic, and see if I can solve my problem. I will get back to you later.

Thank You

OK
Here is a sketch that you need for one input.
Try to see if you can follow things.

``````byte output1 = 4;
byte input1 = A0;
byte hysteresis = 5;

int sensorValue = 0;

boolean enableFlag = true; //when true allow for checking
boolean timingFlag = false;

unsigned long toggleMillis;
unsigned long toggleTime = 1000UL;

void setup()
{
//Serial.begin(9600);
pinMode(output1, OUTPUT);
} //END of setup()

void loop ()
{
//Serial.println(sensorValue);

//If enabled, is the sensor pressed
if (enableFlag == true && sensorValue >= 512)
{
//Turn on the LED
digitalWrite(output1,HIGH);
//Record the start time the LED turned on
toggleMillis = millis();
//Disable this block of code as we want this to happen ony ONCE when >= 512
enableFlag = false;
//Enable the next block of code to check if it is time to turn off the LED
timingFlag = true;
}

//If we are timing, is it time to turn off the LED?
if(timingFlag == true && millis() - toggleMillis >= toggleTime)
{
//Turn off the LED
digitalWrite(output1,LOW);
//Disable this block of code as we are now finished timing
timingFlag = false;
}

//When not timing, is the sensor released?
if(timingFlag == false && sensorValue < 512 - hysteresis)
{
//When we get under (512 - hysteresis) we can start checking for >= 512 again
enableFlag = true;
}

} //END of loop()
``````

I am trying to compile a script to make an analogue input trigger an digital output. I am confused on how to implement the if command. I would like to have if value of A0 is great than 512 make digital out 4 go High.

``````int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}

void loop() {
if(analogRead(A0) > 512) { //if the analog value of A0 is greater than 2.5V then LED ON
digitalWrite(13, HIGH); //write digital pin 13 high
}
else {