check my first code pls

Kindly, im writing a code for my voltage divider control circuit .....i have :

  1. analog input pin A0 (signal +1v or -1v or 0v) square wave
  2. 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;

void setup () {
serial.begin (9600);
delay (5000);
}

void loop () {
int value=analogRead(analog_in);

if (value = 0) {
digitalWrite(dig1_out,LOW);
digitalWrite(dig2_out,LOW);
}
else if (value = 1000) {
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,HIGH);
}
else if (value < 0){
digitalWrite(dig1_out,HIGH);
digitalWrite(dig2_out,LOW);
}
delay(20);
}

f (value = 1000) {

Should be ==
Same for the comparison with zero.

If an analogRead returns less than zero, I'd be very worried.

serial.begin (9600);

The compiler has probably already told you about that one, and your digital pins need to have their pinMode set to OUTPUT.

Please use code tags when posting code.

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);
}

but less than zero ... i do not have solution about it

It can't happen, so don't worry about it.

This
} } shouldn't happen either.
Always put closing braces on their own line.

Would you please at least try to compile your code before re-posting it ?

sketch_dec29a.ino: In function ‘void setup()’:
sketch_dec29a:6: error: ‘serial’ was not declared in this scope

Also, please hit CTRL-T to correct the code indentation.

now ???

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.

Divider.png

now ???

A question mark is enough.

I see you didn't press CTRL-T.

Code indentation is key to readability.

You also didn't put each } on its own line, as suggested.

If you ask for help, at least follow the directions. Otherwise why should others bother to help ?

CTL+T .... it is perfect thnx bro

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);
}

good idea ....i will try to make the input range 1.5v , 2.5v , 4v instead of -1 , 0 , 1

Good :slight_smile:

Just one more suggestion:

this

if (value >= 0) {
    if (value <= 5) {     // less than 200 mv

IMHO is clearer if written as:

if ( (value >= 0) && (value <= 5)) {

:slight_smile:

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);
}

You may want to check the values you are using for analog in.

If you assume a reference of 5V, and 1024 possible analog values, then you get:

value= 1024 * voltage / 5

If you allow around a +/-200mV from those points you get the following value ranges for your if statements:

2V = 408 ----->   368 to 451
3V = 614 ----->   573 to 656
4V = 819 ----->   778 to 860

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.

Hope this helps...