Go Down

Topic: Sensitive Analogread help. (Read 285 times) previous topic - next topic

Megadeth

Hi!

I was just testing my automatic beverage pump i have been prototyping and found an error. This code below works perfect except that the analog inputs is very sensitive, if i just tap the arduino it will think i put a current on A0 or A1 and thus starting the sequence. Is there some way to add a threshold to the code where i atleast need 4volts for the digitalwrite would accept it as "HIGH"?. I wrote an alternative code also (Very far down) where i used a if statement, this works somehow but very randomly sometimes it works, sometimes it is reversed etc. Im sorry if this might be basic stuff but in the end im a complete noob and this is the first code i wrote on the arduino. Any help would be appreciated

Simple schematic

5v --> pushbutton --> analog input.
digital output --> relay --> valve



Code: [Select]
*/

int input0 = A0;    // select the input for each pushbutton
int input1 = A1;     
int output0 = 13;   // select the output pin for the relay
int output1 = 12;   
int val = 0;        // variable for reading the pin status

int buttonState = 0;       
int lastButtonState = LOW;

void setup() {
  // declare the digitalpins as OUTPUTS:
  pinMode(input0, INPUT); //Cola
  pinMode(input1, INPUT); //Sprite
  pinMode(output0, OUTPUT); //Cola
  pinMode(output1, OUTPUT); //Sprite

 
}

void loop(){
 
  // Cola
 
  // read the state of the pushbutton value:
  buttonState = digitalRead(input0);
 
  if (buttonState == HIGH) {     
    // turn relay on:   
    digitalWrite(output0, HIGH);
    delay(5000); 
  }
  else {
    // turn Relay off;
    digitalWrite(output0, LOW);
  }
   
   // Sprite
   
   // read the state of the pushbutton value:
   buttonState = digitalRead(input1);
   
   if (buttonState == HIGH) {     
    // turn Relay on;   
    digitalWrite(output1, HIGH);
    delay(5000); 
  }
  else {
    // turn Relay off;
    digitalWrite(output1, LOW);
  }

}



Alternative Code
------------------------------------------------------------------------------

Code: [Select]
*/

int input0 = A0;    // select the input for each pushbutton
int input1 = A1;   
int output0 = 13;   // select the output pin for the relay
int output1 = 12;     
int value0 = 0;        // variable for reading the pin status
int value1 = 0;        // variable for reading the pin status

void setup() {
  analogReference(DEFAULT);
  pinMode(input0, INPUT); //Cola
  pinMode(input1, INPUT); //Sprite
  pinMode(output0, OUTPUT); //Cola
  pinMode(output1, OUTPUT); //Sprite

 
}

void loop(){
 
  // Cola
 
  int value0 = analogRead(input0);
  if(value0 > 700) digitalWrite(output0, HIGH);   
  delay(5000);
  digitalWrite(output0, LOW);
 
  int value1 = analogRead(input1);
  if(value1 > 700) digitalWrite(output1, HIGH);   
  delay(5000);
  digitalWrite(output1, LOW);
}



johnwasser

Code: [Select]
[code][quote]5v --> pushbutton --> analog input.

There's your problem.  With the switch open the input floats.  You need a pull-down resistor from the pin to ground:

Quote
5v --> pushbutton --> analog input --> 1K to 10K resistor --> Ground [/code]
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy