Kindly, im writing a code for my voltage divider control circuit .....i have :
analog input pin A0 (signal +1v or -1v or 0v) square wave
two digital outputs (+ 5v or 0v) for each one ... so i hv 4 states 0 0,0 1,1 0,1 1
Now :
if A0 = 0 v =====>output pins = 0 0
if A0 = +1 v =====>output pins = 1 1
if A0 = -1 v ======>output pins =1 0 or 0 1
int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;
AnalogRead will never return a negative number. If you try and feed -1V into an analog pin you will damage the Arduino.
Secondly, there is always a margin for error with ADC's. The value will not reliably be 1000, for example it may be 997, 999, 1003 - You should allow a range of numbers to be included.
Next, where did 1000 come from? The analogRead value returns a number between 0 and 1023 which represent equal steps between 0v and AREF. If you are using the default reference voltage then 1023=5v, 0=0v, and approximately 204=1v.
see now ... but less than zero ... i do not have solution about it
int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;
void setup () {
serial.begin (9600);
pinMode(dig1_out, OUTPUT);
pinMode(dig2_out, OUTPUT);
delay (5000);
}
void loop () {
int value=analogRead(analog_in);
if (value >= 0) {
  if (value < 5) {  // less than 200 mv
 digitalWrite(dig1_out,LOW);
 digitalWrite(dig2_out,LOW);
} }
  else if (value <= 209) {
   if (value >= 200) {
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,HIGH);
} }
else if (value < 0){
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,LOW);
}
delay(20);
}
int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;
void setup () {
Serial.begin(9600);
pinMode(dig1_out, OUTPUT);
pinMode(dig2_out, OUTPUT);
delay (5000);
}
void loop () {
int value=analogRead(analog_in);
if (value >= 0) {
  if (value <= 5) {  // less than or equal 200 mv
 digitalWrite(dig1_out,LOW);
 digitalWrite(dig2_out,LOW);
} }
  else if (value <= 209) {  //about 1 v
   if (value >= 200) {
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,HIGH);
} }
else if (value < 0){Â Â Â Â Â Â Â // here is the problem right ?? i need -1 v what i do pls?
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,LOW);
}
delay(20);
}
You will find that the way you have built your if-elseif statements won't work quite as you expect:
if (value >= 0) {
  //value is always greater than zero, so code always reaches here
  if (value <= 5) {
   ...
  }
} else if (value <= 209) {Â Â //about 1 v
  //The first if statement is always true, therefore this bit of code never executes.
  if (value >= 200) {
   ...
  }
}
You are along the right lines though. You just need to consider what you are looking for. For the first if statement you want the value to be >=0 AND <= 5. So based on that you get this:
if ((value >= 0) && (value <= 5)) {
... //will execute if the value meets both conditions
} else if ((value >= 200) && (value <= 209)) {Â Â //about 1 v
...
}
The problem of negative values however still remains. What you need to do is to take your +/-1v signal and add a bias so that it is within the range of the ADC.
There is one such method which uses just two equally sized resistors. I have attached a schematic.
What the circuit does is att 2.5v onto the signal meaning that when you have -1v, the ADC sees 1.5V, when you have +1v, the ADC sees 3.5v.
How successful this method will be depends on what is driving the +/-1V signal.
int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;
void setup () {
 Serial.begin (9600);
 pinMode(dig1_out, OUTPUT);
 pinMode(dig2_out, OUTPUT);
 delay (5000);
}
void loop () {
 int value=analogRead(analog_in);
 if (value >= 0) {
  if (value <= 5) {  // less than 200 mv
   digitalWrite(dig1_out,LOW);
   digitalWrite(dig2_out,LOW);
  }
 }
 else if (value <= 209) {
  if (value >= 200) {
   digitalWrite(dig1_out,HIGH);
   digitalWrite(dig2_out,HIGH);
  }
 }
 else if (value < 0){
  digitalWrite(dig1_out,HIGH);
  digitalWrite(dig2_out,LOW);
 }
 delay(20);
}
voltage shifted to (2 v,3v,4v) where 3v is the reference ... see the code and tell me
int analog_in = A0;
int dig1_out = 9;
int dig2_out = 10;
void setup () {
 Serial.begin (9600);
 pinMode(dig1_out, OUTPUT);
 pinMode(dig2_out, OUTPUT);
 delay (5000);
}
void loop () {
 int value=analogRead(analog_in);
 if ((value >= 428 ) && (value <= 618)) // refrence range (2.8v - 3.2v)
 {
  digitalWrite(dig1_out,LOW);
  digitalWrite(dig2_out,LOW);
 }
 else if ((value >= 812) && (value <= 818)) { //up range (3.8v - 4 v)
  digitalWrite(dig1_out,HIGH);
  digitalWrite(dig2_out,HIGH);
 }
 else if ((value >= 404) && (value <= 410)) { //down range (2v - 2.2v)
  digitalWrite(dig1_out,HIGH);
  digitalWrite(dig2_out,LOW);
 }
 else {                  // for none of the above states
  digitalWrite(dig1_out,LOW);
  digitalWrite(dig2_out,LOW);
 }
 delay(20);
}
Perhaps you could use a multimeter and a simple analog-to-serial sketch, so you can directly check voltage and analog reads.
There should be an example in the ide, anyway it's as simple as:
void setup() {
  Serial.begin(9600);  // or whatever speed you prefer
}
void loop() {
  int value;
  value = analogRead(0);  // use the correct number for your hardware
  Serial.println(value);
  delay(1000);
}
Upload and open the serial monitor, then keep watching the multimeter readings and the serial values on the screen.