Implementing a Button

I'm trying to implement a button but I don't know how D: please help

Codes:
int referenceVoltage = 5; // use the default voltage as a reference
int initialReading; // raw value pulled of analog pin
float initialVoltage; // real voltage converted from reading
int analogValue; // store the analog reading here
int analogPin = 0; //use A0 as the analog pin
int shortPin = 3; // use pin D0 to turn on the transistor which will short the battery
int startButton = 4; // Use D4 pin to register when a button is pushed.
const int numberOfMeasurements = 20; // take 20 readings each time
int Stored[numberOfMeasurements];
int numReadings = 200; //Change this when you know how many readings you want!!!!!!
float volt;
int refvolt = 5;
int msec = 0;

void setup() {
delay(50);
initialReading = analogRead(analogPin); // get the battery's initial voltage
initialVoltage = initialReading * referenceVoltage / 1024;
Serial.begin(9600);
if (analogPin); {
Serial.print(analogPin);
msec = millis();
}
}

void loop() {
for (int i = 0; i < numReadings; i++) {
volt = volt + analogRead(analogPin);
}
digitalWrite(shortPin, HIGH);
delay(500);
volt = volt / numReadings / 1024 * refvolt;
Serial.print(volt);
Serial.println('Battery Recovery Profile');
volt = 0;
delay(10); {
Serial.print(millis() - msec);
Serial.print(" ");
}
}

EDIT: I want the button to control when the timer starts.

Please use the code button "</>" when posting code.

I don't see where you want to use the button in your code and you have not explained what you want to happen.

Have you looked at the button examples that come with the Arduino IDE ?

...R

Okay, I'll use the code button next time, sorry, new to all this stuff and yes, I have looked at the button examples but I still don't get it (sorry).

So, I haven't put in any codes for the button in either void setup/loop because I'm not sure what I'm supposed to put in. What I want to happen is, when I use the button, time ( Serial.print(millis() - msec); Serial.print(" "):wink: will begin to be displayed in the serial monitor. I hope that clears things up and thank you for the reply.

EPICNOMNOMNOMTIME:
I hope that clears things up and thank you for the reply.

Not really. I think it would be easier if you make an attempt to include the button code in your program and post the revised program.

You may find some useful ideas in planning and implementing a program.

...R

Hi,

What I want to happen is, when I use the button, time ( Serial.print(millis() - msec); Serial.print(" ");) will begin to be displayed in the serial monitor.

Please read the above quote, I have just dropped in from Mars and have no idea what you are talking about.
Please tell us

  • What your application is.
  • What you want the program to do, as in input and outputs.
  • What you want the buttton to do.

Then read what you have written, if it sounds logical then post it.

Also,
Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Hope to help... Tom....... :slight_smile: