Analogue input help!


I am very new to Arduino, although fairly experienced with C++.

I am having a bit of trouble with some code which has a pretty simple purpose: The input of a phone vibration motor is connected to the analogue input A-0 and ground, and I am running an if statement which tries to identify if the PD increases to above 1.5v across the two conductors. This is then supposed to activate a digital output for 30 seconds, however my if statement seems to be constantly true. The code is below. I would be really thankful if somebody might be able to explain what I may have done wrong? I'm not sure if it is a code issue of that my electronics are not quite right (I studied electrical power engineering, not electronic!). As it is analogue, I haven't bothered with a pull-down resistor.



int phonepin = 0; int relaypin = 13; float voltage; float threshold = 1.5;

void setup() { pinMode(phonepin, INPUT); pinMode(relaypin, OUTPUT); Serial.begin(9600);

} void loop() { voltage = analogRead(phonepin)* 5 / 1023; Serial.print(voltage); delay(500); Serial.print("\t"); digitalWrite(relaypin, LOW);

if (voltage > threshold); { digitalWrite(relaypin, HIGH); delay(30000); digitalWrite(relaypin, LOW); delay(500); }


You're not going to share your serial output? That's not very nice.

Hi Paul S,

What do you mean?


if (voltage > threshold);


This is the condition I am looking for in order for pin 13 to go high for a period of time. Should I be writing this differently, or is it inappropriate to use floats?


Inappropriate use of semicolon.

Thanks AWOL, what a silly mistake.

You're not the first, and I don't imagine you'll be the last ;) Enjoy!

Nice app to start with, did you connect directly to the motor or did you use some sensor - so it would work with any vibrating phone?

General remarks: - please use the # button above the smiley's to tag your code , also doing a CTRL-T in the IDE does a reindent => looks better

more important, you should do the voltage math in float while now it is done in integer See code below, I applied some other thingies too - header - consts where appropriate - removed one delay to make system more reactive (other is homework ;) - and the float math where appropriate (code not tested)

//    FILE: PhoneBuzzDetector.pde
//  AUTHOR: Sam Glasius
//    DATE: 2012-12-27

const int phonepin = A0;    // made const as they will not change   #define also a good option
const int relaypin = 13;   // idem

float voltage = 0;  // could be local within loop, minimize scope 
float threshold = 1.5;

unsigned long lastOutput = 0;
#define DISPLAY_THRESHOLD  500UL   // 2x per second

void setup()
  pinMode(phonepin, INPUT);

  pinMode(relaypin, OUTPUT);
  digitalWrite(relaypin, LOW);

  Serial.println("PhoneBuzzDetector 0.1"); // to recall what is loaded on the Arduino 

void loop()
  voltage = analogRead(phonepin) * 5.0 / 1023;

  //without delay
  unsigned long now = millis();
  if (now - lastOutput >= DISPLAY_THRESHOLD)
    lastOutput += DISPLAY_THRESHOLD;
    Serial.print(voltage, 3);  // use 3 digits
  // the delays can be removed here similar to above in the display part
  // by holding the state of the relay, previous state and a time stamp
  if (voltage > threshold)  
    digitalWrite(relaypin, HIGH);
    digitalWrite(relaypin, LOW);
float voltage;
float threshold = 1.5;
  voltage = analogRead(phonepin)* 5 / 1023;
  if (voltage > threshold);   {

The Arduino doesn't have an FPU, so any floating point calculations are done through software emulation and are very taxing on these modest devices. In this example, you're doing unnecessary floating point calculations. This could be made much more efficient by just converting 1.5v to the expected 10-bit ADC value:

int voltage;
int threshold = 306; // 1.5 * 1023 / 5
  voltage = analogRead(phonepin);
  if (voltage > threshold) {


+1 good point, That saifd, for the given application float will still be fast enough .. (one can do quite some float math in delay(500))

one can do quite some float math in delay(500)

No, one cannot. (but I know what you're saying)

You got me AWOL ;)

Rephrase: One can do quite some float math in the 500 milliseconds a delay(500) takes.