The first lab was to get us acclimated to Arduino and provide us with the foundations to start developing medical devices.
Part 1:
The first part of this lab was simply downloading the program and then mounting the LCD to a "backpack" so that building a circuit with the LCD would not be a mess of wires every time we used it. Mounting the LCD to the backpack involved some soldering - which was a little hard to get used to!
The LCD mounted onto the backpack. As you can see, there are only 4 wires required to connect to the Arduino.
The underside of the LCD. If you look closely, you can see the great job Maddie did at soldering.
Part 2
Serial Output:
This portion of the lab introduced us to "sketches" and how Arduino libraries work. This is necessary for later when we need Adafruit.LiquidCrystal to use the LCD screen.
The first coding we did involved printing a phrase to the Serial Monitor.
Then, we moved onto using variables - still all within the Serial Monitor.
As you can see, the code says to print the string "my number is" and then the variable "x" - which is equal to 44, in this case.
The next step involved having our variables change with each iteration of the loop.
In this code the "for" loop has the variable "i" which changes upon each iteration. In hindsight, the "int x = 44" was unnecessary but this was left over from the previous exercise.
A screenshot of the Serial Monitor printing the above code. As you can see, the loop counts up one at a time until it reaches 15 and then begins again from 0.
My First Hardware Project - Digital Output
This is our completed LED circuit with a red LED light and a 470-ohm resistor in series to limit the current.
The above is the code used to blink the LED on and off.
LED Blinks Faster
LED Blinking in Alternation
Second Hardware Project - Digital Input
The next hardware project was to build a circuit with a pushbutton and to read its state of being pressed vs non-pressed.
This is our circuit with the 10k Ohm resister. The resister is there to limit current flow from +5V directly to ground when the pushbutton is pressed.
This is the code we used to read the button state (of being pushed or not). The digitalRead of a button when unpressed is "1," and when it is pressed it is "0."
Next, we began looking at if-else loops.
Using the LCD
In order to use the LCD, we downloaded the Adafruit Liquid Crystal library. In order to include the library when coding, it is imperative that we include lines such as
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
Counting Time
We first used the delay function as suggested. However, we noticed that there was too much of a delay when the numbers were printing to the Serial monitor.
Our code when we counted milliseconds using the millis() function. This proved to be much more accurate.
Reaction-Time Measuring Device Our next objective was to make a reaction-time measuring device where a microcontroller would turn an LED on, and then measure and display how long it took for the subject to press a pushbutton. The reaction time would then be displayed on the LCD. For this lab we knew we would have to use the Adafruit Liquid Crystal library, so we were sure to immediately include that library and wires when writing the code.
With this code, we had a pushbutton and an LED connected. The LED would turn on to signal to the user to press the push button, at which point there would be a digital read of the button state. If the button was pressed, the elapsed time, or reaction time, would display on the LCD.
We had some issues with our LCD, even after adjusting the potentiometer, leading us to believe there may have been an issue with the soldering.
Activity Monitoring Device For this lab, we used the pushbutton to act as a sort of pedometer that would monitor the number of steps taken in a time interval and to alert the user if the number recorded is less than the desired one.
int ledPin=10;
int buttonPin=8;
int timer=0;
int steps=0;
int threshold = 10;
int buttonState;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState=digitalRead(buttonPin);
timer=millis(); //Keeps track of time (ms)
if (timer<=5000)
if (buttonState==0) //button is pressed
{
steps=steps++;
digitalWrite(ledPin,LOW);
Serial.print(steps);
Serial.print("\n");
}
else
{
digitalWrite(ledPin,HIGH);
Serial.print("Button not pressed");
Serial.print("\n");
delay(500);
}
We uploaded this code as text because there was too much to screenshot. We do not have a video of this accurately working as we ran into some problems with the if/else loops. We used the same circuit as we did for the reaction time monitor above with the exception of switching over to using the Serial Monitor rather than LCD as there were problems with the display.
Use Serial.read() to control the Arduino We learned we needed to enter or "send" the data to the serial monitor each time we input new data. Play Notes on the Arduino int inputChar; void setup () { pinMode(12,OUTPUT); Serial.begin(9600); } // this code will play the A major scale which has 3 sharps, C#, F#, and G# void loop(){ if (Serial.available() > 0) { inputChar = Serial.read(); if (inputChar == 'a'){ tone(12,1760); } if(inputChar == 'b'){ tone(12,1976); } if(inputChar == 'c'){ tone(12,2216); } if(inputChar == 'd'){ tone(12,2348); } if(inputChar == 'e'){ tone(12,2636); } if(inputChar == 'f'){ tone(12, 2960); } if(inputChar == 'g'){ tone(12, 3324, 400); } } } Above is the code we used to play different tones corresponding to an A major scale. We used a Piezo buzzer and had the same circuit set-up as in previous labs. Create some LED flashing r...
Build a data-logger with a photoresister Below is our code for the data logger with a photoresistor: #include <EEPROM.h> int pin = A0; int index = 0; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(pin,INPUT); Serial.println("Input 'W' to write data into EEPROM. Input 'R' to read data."); } void loop() { // put your main code here, to run repeatedly: char inputChar = Serial.read(); if (inputChar == 'W'){ for(index; index <13; index ++){ int data = analogRead(pin); EEPROM.write(index, data); index ++; delay(5000); } index = 0; }else if (inputChar == 'R'){ int i = 0; while (i <13){ int value = EEPROM.read(i); ...
ECG Tutorial Heart rate from ECG in Arduino - sketch that acquires the ECG signal via one of the analog inputs #include "Wire.h" #include "Adafruit_LiquidCrystal.h" Adafruit_LiquidCrystal lcd(0); int pin = A0; int count = 0; void setup() { Serial.begin(9600); // put your setup code here, to run once: pinMode(pin, OUTPUT); lcd.begin(16,2); } void loop() { // put your main code here, to run repeatedly: int now = millis(); while (millis() - now < 10000) { int sensorValue = analogRead(pin); Serial.println(sensorValue); if (sensorValue > 116) { count++; } } int printed = count*6; lcd.print(printed); lcd.print("bpm"); delay(300); lcd.clear(); count = 0; } - devise a procedure to calculate the heart rate from the acquired ECG signal - send the heart rate to the LCD Pulse Sensor Here is a screenshot of what the Pulse S...
Comments
Post a Comment