Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« on: December 29, 2012, 05:35:13 am » |
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
Online
Brattain Member
Karma: 137
Posts: 19007
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: December 29, 2012, 05:36:17 am » |
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.
|
|
|
|
« 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.
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 982
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #2 on: December 29, 2012, 05:41:46 am » |
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
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #3 on: December 29, 2012, 06:32:59 am » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19007
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: December 29, 2012, 06:37:11 am » |
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.
|
|
|
|
« 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.
|
|
|
|
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)
|
 |
« Reply #5 on: December 29, 2012, 07:00:20 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #6 on: December 29, 2012, 07:14:41 am » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 982
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #7 on: December 29, 2012, 07:33:02 am » |
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.
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
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)
|
 |
« Reply #8 on: December 29, 2012, 07:46:32 am » |
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
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #9 on: December 29, 2012, 12:15:07 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #10 on: December 29, 2012, 12:32:32 pm » |
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
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #11 on: December 29, 2012, 12:45:46 pm » |
Good  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)) {

|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 77
|
 |
« Reply #12 on: December 29, 2012, 03:53:21 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 982
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #13 on: December 29, 2012, 04:04:07 pm » |
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
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
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)
|
 |
« Reply #14 on: December 29, 2012, 09:16:08 pm » |
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...
|
|
|
|
|
Logged
|
|
|
|
|
|