ASSIGNMENT
Measure something: add a sensor to a microcontroller board that you've designed and read it.
WHAT I DID
Using the hello world board I wanted I read back the vibrations pulses detected by the vibration sensor.
HARDWARE
I needed an FTDI cable and my FabISP.
and connect: -- usb port / FabISP / Hello World board / usb port --
CODE
The code that allowed me to print on the monitor values from my board comes from the library SoftwareSerial.h. You need do define the TX and RX values pin (that goes to the 6 pin flat header).
#include <SoftwareSerial.h>
#define rxPin 0
#define txPin 1
SoftwareSerial serial(rxPin, txPin);
In the void setup():
{.. pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
serial.begin(9600); ..}
In void loop() the command to print your text or values on the serial monitor:
{..
serial.println("text");
..}
In my case I wanted to count the numbers of times the vibration sensor that actually is a mechanic switch get open and close. To have a close loop every 5 changes from the sensor the count starts from zero.
#include <SoftwareSerial.h>
#define rxPin 0
#define txPin 1
int sw_pin = 3;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
SoftwareSerial serial(rxPin, txPin);
void setup() {
pinMode(sw_pin, INPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
serial.begin(9600);
}
void loop(){
buttonState = analogRead(sw_pin);
if (buttonState == lastButtonState) {
if (buttonState == 1023) {
buttonPushCounter++;
serial.println("on");
serial.println(buttonPushCounter);
}
else {
serial.println("off");
}
}
lastButtonState = buttonState;
if (buttonPushCounter > 4) {
buttonPushCounter = 0;
delay(1000);
}
}
and this is the result:
|