the analog signal input to arduino (A0) read voltage between (1v-3v) i need a statement do the following (if A0 signal increased then digital output(10) HIGH else if A0 signal decreased then digital output(11) HIGH)
Need a little more defining:
A0 increased from what? Or decreased from what?
What if 10 was already high? Do you ever want it to go low? If so, when?
Same for 11.
How often is this comparison to take place?
read A0
save value to variable oldValue
start of loop
read A0
save value to variable newValue
if oldValue less than newValue
make pin 11 HIGH
else if oldValue greater than newValue
make pin 10 HIGH
end of if
set oldValue equal to newValue
end of loop
There is a shorter way of writing the if/else using the ternary operator but as has been pointed out there are many unanswered questions to be resolved before trying to get too clever
thank u UKHeliBob
but now i have 2 analog inputs , and some times both increased together or decreased , see the signal below ... i want to write a code to make these signals usable to control other device
i want to write a code to make these signals usable to control other device
That's vague.
okay, leave this part ...and help me to write a code to acccept 2 analog inputs (A0 & A2) if A0 increased digital 10 is HIGH if A0 is decreased digital 11 is HIGH ..same thing for A2 with digital outputs 12 & 13
the problem is when both increased , decreased , or one increased and the other decreased
if A0 increased digital 10 is HIGH if A0 is decreased digital 11 is HIGH
Increased or decreased with respect to what?
The last sample?
The other input?
Some running average?
Some fixed threshold?
Some dynamic threshold?
the last sample
So, test the last sample against the current sample, and set or reset the digital pin accordingly.
You'll need a global or local static variable to hold the value of the last sample.
the signal is not stable ( the level is shifted ) ..and u mintioned (dynamic threshold) ..this statement give in my mind an idea :
if the signal shifted change the threshold and compare if the input value is greater than the new threshold or less ...
now i need the statement of daynamic threshold of the analog input ? pls
What have you written so far?
dany151:
okay, leave this part ...and help me to write a code to acccept 2 analog inputs (A0 & A2) if A0 increased digital 10 is HIGH if A0 is decreased digital 11 is HIGH ..same thing for A2 with digital outputs 12 & 13
the problem is when both increased , decreased , or one increased and the other decreased
If you have the single input/2 output code working then just repeat it inside the loop() function with different variable names to hold the old and new analogue values. As before, there are smarter ways of doing this but get the long winded one working first.
I just saw your new post. Please stop moving the goalposts and tell us what you want
im sorry, im being unstable like the analog signal ... but its just an idea to solve my problem ... u can see the signals shape ..in the blue one
the level is 2 v and its go up and down ....if its more than 2v then pin10 HIGH and if less than 2v then pin11 HIGH ...the problemis at the level
sometimes the level being 1v or 1.5v then it will not reach the 2v and the first condition will not work ...also the second condition will be active all the time ... this is my problem !
dany151:
im sorry, im being unstable like the analog signal ... but its just an idea to solve my problem ... u can see the signals shape ..in the blue one
the level is 2 v and its go up and down ....if its more than 2v then pin10 HIGH and if less than 2v then pin11 HIGH ...the problemis at the level
sometimes the level being 1v or 1.5v then it will not reach the 2v and the first condition will not work ...also the second condition will be active all the time ... this is my problem !
You are still not describing the requirement fully
If the input is below 2V when you start the program, what should the outputs be ?
If the input now rises above 2V what should the outputs be ?
If the input now falls below 2V what should the outputs be ?
If the input now rises above 2V again what should the outputs be ?
i will show you my code ... but its not working for fully ..its just working if i use one input ...but when i use both inputs it gives worng outputs
int fpaa1 = A0;
int fpaa2 = A2;
int right = 9;
int left = 10;
int forward = 11;
int back = 12;
int value1= 0;
int value2= 0;
int x = 1;
void setup () {
Serial.begin(9600);
pinMode(right, OUTPUT);
pinMode(left, OUTPUT);
pinMode(forward, OUTPUT);
pinMode(back, OUTPUT);
delay(200);
}
void loop () {
while(x>0) {
value1=analogRead(fpaa1); //value= 1024 * voltage / 5
value2=analogRead(fpaa2); //value= 1024 * voltage / 5
float voltage1 = value1 * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage2 = value2 * (5.0 / 1023.0);
Serial.println(voltage1);
Serial.println(voltage2);
if (voltage1 >= 2.5) {
digitalWrite(forward,HIGH);
digitalWrite(back,LOW);
digitalWrite(left,LOW);
digitalWrite(right,LOW);
break;
}
else if (voltage1 <= 0.9) {
digitalWrite(forward,LOW);
digitalWrite(back,HIGH);
digitalWrite(left,LOW);
digitalWrite(right,LOW);
break;
}
else if (voltage2 >= 2.5) {
digitalWrite(forward,LOW);
digitalWrite(back,LOW);
digitalWrite(left,HIGH);
digitalWrite(right,LOW);
break;
}
else if (voltage2 <= 0.9) { //down range (2v - 2.2v)
digitalWrite(forward,LOW);
digitalWrite(back,LOW);
digitalWrite(left,LOW);
digitalWrite(right,HIGH);
break;
}
else { // for none of the above states
digitalWrite(forward,LOW);
digitalWrite(back,LOW);
digitalWrite(left,LOW);
digitalWrite(right,LOW);
break;
}
}
delay(20);
}
I'm really not clear from your description what you're trying to do, but looking at the code, it seems that voltage1 controls the forward and back (directions?) and voltage2 does left and right. This line though:
else if (voltage2 >= 2.5) {
Means that you only consider left and right if the forward and back conditions aren't met. Is that what you wanted?
YES...
i did some conditions like if (voltage1 >= 2.5 && voltage2 <=2) {
...but it is not working good ...so im trying to write the code in another way
You start a series if if/else statements checking voltage1 the, in the same if/else start testing voltage2 so if any of the voltage1 conditions are true the test of voltage2 will not happen. Split the voltage checks into 2 blocks of if/else. Test voltage1 and take action then in a separate if/else test voltage2 and take action.
good idea UKHeliBob ... i will try to split them and check ... i hope this solve my problem ...i appreciate your help..thnx