Pages: [1]   Go Down
Author Topic: check my first code pls  (Read 1018 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
f (value = 1000) {
Should be ==
Same for the comparison with zero.

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

Code:
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.
« Last Edit: December 29, 2012, 05:43:13 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

~Tom~

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26348
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but less than zero ... i do not have solution about it
It can't happen, so don't worry about it.

This
Code:
} }
shouldn't happen either.
Always put closing braces on their own line.
« Last Edit: December 29, 2012, 06:38:50 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

now ???

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

Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You will find that the way you have built your if-elseif statements won't work quite as you expect:
Code:
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:
Code:
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 (3.78 KB, 326x170 - viewed 27 times.)
Logged

~Tom~

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 ?
« Last Edit: December 29, 2012, 07:48:05 am by tuxduino » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good smiley

Just one more suggestion:

this

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

IMHO is clearer if written as:

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

smiley
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

voltage shifted to (2 v,3v,4v) where 3v is the reference ... see the code and tell me

Code:

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















Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1726
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
2V = 408 ----->   368 to 451
3V = 614 ----->   573 to 656
4V = 819 ----->   778 to 860
Logged

~Tom~

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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...
Logged

Pages: [1]   Go Up
Jump to: