Go Down

Topic: check my first code pls (Read 1 time) previous topic - next topic

tuxduino

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

Code: [Select]

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.

dany151

now ???

Code: [Select]

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


Tom Carpenter

You will find that the way you have built your if-elseif statements won't work quite as you expect:
Code: [Select]
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: [Select]
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.
~Tom~

tuxduino

#8
Dec 29, 2012, 01:46 pm Last Edit: Dec 29, 2012, 01:48 pm by tuxduino Reason: 1
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 ?

dany151

CTL+T .... it is perfect thnx bro
Code: [Select]


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



Go Up