Lesson 8--Microduino "Pulse Recorder"
Language | English |
---|
目录ObjectiveIn lesson 3, you learned how to use a button and avoid the issue of electrical noise. The solution was to add a short delay. However, this solution has a disadvantage. Different people will take different amounts of time to press a button. That means the delay must be different for different people, too. This lesson will teach you how to get the duration that a button is pressed down. This program uses pulse timing calculation. In addition, you will learn about Arduino's serial port to monitor the data. Equipment
PulsePulse is a rapid, transient change in the amplitude of a signal from a baseline value to a higher or lower value, followed by a rapid return to the baseline value. In Microduino, pulse is simply a digital signal which changes between high and low in a cyclical pattern. Pulse timing is often used to calculate the speed in electronic components. Experiment SchematicThis schematic uses the internal pull-up. We also connected an external ceramic capacitor for stabilization. Programint pin = 2; //Define Pin D2
float time1,time2; //Define variables to float
void setup()
{
Serial.begin(115200); //Serial port baud rate
pinMode(pin, INPUT_PULLUP); //Set pin to input mode with the internal pull-up
}
void loop()
{
//Read low pulse from the pin, the maximum pulse interval is 60 seconds, and assign the result to the variable time1
time1= pulseIn(pin, LOW, 60000000)/1000; //Convert the time to ms
Serial.print(time1); //Output the time1 by serial
Serial.print("ms ");
time2= pulseIn(pin, LOW,60000000)/1000.0; //Convert the time to ms
Serial.print(time2); //Output time1 by serial
Serial.println("ms"); //Output time2 by serial and start a new line
}
Serial MonitoringOpen the serial port monitor
pulseIn() usage
ResultFor stabilization, add a ceramic capacitor in the signal change port. You will be able to see better results. Video |