Commit ec12e7ce authored by lucy.hemingway's avatar lucy.hemingway

Add new file

parents
//pins used for sensors
#define trigPin 12
#define echoPin 13
#define fsrpin A1
#include <SPI.h>
#include <WiFiNINA.h>
#include <ThingSpeak.h>
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth (0,1);
int redLed = 2; // Set green LED to pin 3, red to pin 2
int fsrreading = 0;
int bluetoothinput;
// wifi connection
// with reference to The Internet of Things 3CB105 Week 6 Lecture "Arduino Uno WiFi connection"
const char ssid[] = "TALKTALKE2531E";
const char pass[] = "WB8WH3H7";
const unsigned long channel_id = 1592248;
const char write_api_key[] = "VN2B96JBUY6HUPX7";
WiFiClient client;
void setup() {
int status = WL_IDLE_STATUS;
Bluetooth.begin (9600);
Bluetooth.println("Attempting to connect.");
status = WiFi.begin(ssid,pass);
if (status != WL_CONNECTED) {
Bluetooth.println("Not connected, try again.");
while (true);
}
else{
Bluetooth.print("Connected to ");
Bluetooth.println(ssid);
Bluetooth.print("with IP address: ");
Bluetooth.println(WiFi.localIP());
ThingSpeak.begin(client);
}
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLed, OUTPUT);
Bluetooth.println ("Bluetooth connected, please press 0 to turn off, or press 1 to get last reading: ");
Bluetooth.println("Distance and Force readings from your device: ");
}
void loop() {
delay(500);
//bluetooth connection - with reference to https://gist.github.com/ashinjohn/9fcc9362dc4787ae08c030c2e7401f4c - accessed on 11/12/2021
if (Bluetooth.available()){
bluetoothinput = Bluetooth.read();
if (bluetoothinput == '0'){
Bluetooth.println("Device turned off, please reset device to start again");
exit(0);
}
}
// programmed Ultrsonic sensor with reference to - https://create.arduino.cc/projecthub/MisterBotBreak/how-to-use-an-ultrasonic-sensor-181cee
// code to turn on and off the sound and work out the distance by multiplying by 0.034 and dividing by 2
delay(100);
int duration, distance = 0, i;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Trig pin sends a ping
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Echo receives the ping
//distance = (duration / 2) / 29.1;
distance = duration * 0.034 / 2;
Bluetooth.println("");
Bluetooth.println("Distance = " + String(distance) ); //print the distance
if (distance > 10){
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
Bluetooth.println("Sit back!");
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
}
//FSR code with reference to - https://www.makerguides.com/fsr-arduino-tutorial/
fsrreading = analogRead(fsrpin); //read pin
Bluetooth.println("Force Reading = " + String(fsrreading)); //print the force
// show different wording depending on the force read to compare different levels
// the wording has now beeen shortened so not much is shown on the tera term unless important
// readings starting at 90 as there is a slight amount of pressure felt by the fsr with no touch.
if (fsrreading <= 90) {
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
Bluetooth.println("Not being touched! Sort out your posture");
Bluetooth.println();
Bluetooth.println();
Bluetooth.println();
}
Bluetooth.println("Posting " + String(fsrreading) + " to ThingSpeak"); // declare posting pressure to thingspeak
ThingSpeak.setField(2, String(fsrreading));
ThingSpeak.writeFields(channel_id,write_api_key); //post to thingspeak
// If sensor detects object further than 8 cm or a force less than 200 then the LED will turn on
// to show that the posture is not good
if (distance >= 10 || fsrreading <= 90) {
digitalWrite(redLed, HIGH); // Turn on red LED
delay(500);
}
// else
else {
digitalWrite(redLed, LOW); // Turn off red LED
}
delay(2000); //delay before collecting the next set of data
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment