From 1e96df05d7457943bb66ff86d82f935c0aaacd89 Mon Sep 17 00:00:00 2001 From: Thomas White <whitet11@cardiff.ac.uk> Date: Fri, 5 May 2023 12:17:22 +0000 Subject: [PATCH 1/2] Upload New File --- cwbackup.ino | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 cwbackup.ino diff --git a/cwbackup.ino b/cwbackup.ino new file mode 100644 index 0000000..d56b1d5 --- /dev/null +++ b/cwbackup.ino @@ -0,0 +1,153 @@ +//ble is on the UART +// motion sensor is on 4 +// range finder is on 8 +// button is on 5 +// buzzer is on 3 + +// set up motion sensor and create motion detection variable +#define PIR_MOTION_SENSOR 4 +#define BUZZER_PIN 3 +#include <Ultrasonic.h> +Ultrasonic ult2(8); + + +// code for the melody +int length = 7; +char notes[] = "abcdef"; +int beats[] = {1,1,1,1,1,1}; +int tempo = 300; + +boolean silentMode = true; +int MotionState; +long range = 0; + + + +void setup() { + // ASSIGN PINS SENSORS + pinMode(PIR_MOTION_SENSOR, INPUT); + pinMode(3, OUTPUT); + pinMode(BUZZER_PIN, OUTPUT); + Serial.begin(9600); + + //BLUETOOTH SETUP + while(!Serial); // this but hangs the bluetooth until we open the monitor + setupBlueToothConnection(); + +} + + + + +void loop() { + // Loop kept simple and decluttered for easy reading + blueToothCall(); + motionSense(); //call motion sensing method +} +// END + + + +// FUNCTIONS +void readUltrasonic(){ + range = ult2.MeasureInCentimeters(); +} + +void motionSense(){ + // read data from sensors + MotionState = digitalRead(PIR_MOTION_SENSOR); + // Motion Sensing + if((MotionState == 1) && !silentMode){ + // Serial.println("Movement"); + digitalWrite(3,HIGH); // make noise + delay(500); + //digitalWrite(3,LOW); // stop making noise + delay(500); + }else if((MotionState == 1) && silentMode){ + // + Serial.println("Motion"); + readUltrasonic(); + Serial1.println(range); + //Serial1.println("Motion"); + delay(500); + }else{ + Serial.println("Watching"); + //Serial1.println("Watching"); + delay(3000); + } +} + +void song(){ + // play song on startup + for (int i = 0; i < length; i++){ + if (notes[i] == ' '){ + delay(beats[i] * tempo); + }else{ + playNote(notes[i], beats[i] * tempo); + } + delay(tempo / 2); // delay between notes + } +} + +// BLUETOOTH SECTION +String SerialString() +{ + String inputString = ""; + while (Serial.available()){ + char inputChar = (char)Serial.read(); + inputString += inputChar; + } + return inputString; +} + +void setupBlueToothConnection() +{ + Serial1.begin(9600); + Serial1.print("AT"); + delay(400); + Serial1.print("AT+ROLE0"); // set the role as peripheral. + delay(400); + Serial1.print("AT+NAMEGroup7IOT"); // set the name as Ratata. + Serial1.flush(); +} + +void blueToothCall(){ + //BLUETOOTH SECTION + char recvChar; + String WholeCommand = ""; + /*DEBUG START*/ + while(Serial1.available()){ + recvChar = Serial1.read(); + Serial.print(recvChar); + } + while(Serial.available()){ + WholeCommand = SerialString(); + Serial1.print(WholeCommand); + delay(400); + } +} + + + +// play tone +void playTone(int tone, int duration) { + for (long i = 0; i < duration *1000L; i += tone *2){ + digitalWrite(BUZZER_PIN, HIGH); + delayMicroseconds(tone); + digitalWrite(BUZZER_PIN, LOW); + delayMicroseconds(tone); + } +} + +void playNote(char note, int duration){ + char names[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'A'}; + int tones[] = {2109, 1879, 1774, 1580, 1408, 1329, 1184, 1054}; + + + + for (int i = 0; i < 8; i++){ + if (names[i] == note){ + playTone(tones[i], duration); + } + } +} -- GitLab From d7c669fad3c51c25fa73334a4d099d06029ac8f2 Mon Sep 17 00:00:00 2001 From: Kieran McKendrick <mckendrickk@cardiff.ac.uk> Date: Fri, 5 May 2023 12:46:27 +0000 Subject: [PATCH 2/2] Added button Functionality --- cwWorking.ino | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 cwWorking.ino diff --git a/cwWorking.ino b/cwWorking.ino new file mode 100644 index 0000000..ae7c233 --- /dev/null +++ b/cwWorking.ino @@ -0,0 +1,180 @@ +//ble is on the UART +// motion sensor is on 4 +// range finder is on 8 +// button is on 5 +// buzzer is on 3 + +// set up motion sensor and create motion detection variable +#define PIR_MOTION_SENSOR 4 +#define BUZZER_PIN 3 +#define LED 5 +#include <Ultrasonic.h> +Ultrasonic ult2(8); + + +// code for the melody +int length = 7; +char notes[] = "abcdef"; +int beats[] = {1,1,1,1,1,1}; +int tempo = 300; + +boolean silentMode = true; +int MotionState; +long range = 0; +const int buttonPin = 5; +int buttonState; +bool buttonLedState= true; + + +void setup() { + // ASSIGN PINS SENSORS + pinMode(PIR_MOTION_SENSOR, INPUT); + pinMode(3, OUTPUT); + pinMode(BUZZER_PIN, OUTPUT); + Serial.begin(9600); + + //BLUETOOTH SETUP + while(!Serial); // this but hangs the bluetooth until we open the monitor + setupBlueToothConnection(); + +} + + + + +void loop() { + // Loop kept simple and decluttered for easy reading + modeCheck(); + blueToothCall(); + motionSense(); //call motion sensing method +} +// END + + + +// FUNCTIONS +void modeCheck(){ + if (digitalRead(buttonPin) == HIGH){ + silentMode = !silentMode; + + if(silentMode){ + digitalWrite(LED, HIGH); + digitalWrite(3,HIGH); + delay(100); + digitalWrite(3,LOW); + delay(500); + } + else{ + digitalWrite(LED, LOW); + digitalWrite(3,HIGH); + delay(50); + digitalWrite(3,LOW); + delay(500); + } + } +} + + + +void readUltrasonic(){ + range = ult2.MeasureInCentimeters(); +} + +void motionSense(){ + // read data from sensors + MotionState = digitalRead(PIR_MOTION_SENSOR); + // Motion Sensing + if((MotionState == 1) && !silentMode){ + // Serial.println("Movement"); + digitalWrite(3,HIGH); // make noise + delay(100); + digitalWrite(3,LOW); // stop making noise + delay(500); + }else if((MotionState == 1) && silentMode){ + // + Serial.println("Motion"); + readUltrasonic(); + Serial1.println(range); + //Serial1.println("Motion"); + delay(500); + }else{ + Serial.println("Watching"); + //Serial1.println("Watching"); + delay(3000); + } +} + +void song(){ + // play song on startup + for (int i = 0; i < length; i++){ + if (notes[i] == ' '){ + delay(beats[i] * tempo); + }else{ + playNote(notes[i], beats[i] * tempo); + } + delay(tempo / 2); // delay between notes + } +} + +// BLUETOOTH SECTION +String SerialString() +{ + String inputString = ""; + while (Serial.available()){ + char inputChar = (char)Serial.read(); + inputString += inputChar; + } + return inputString; +} + +void setupBlueToothConnection() +{ + Serial1.begin(9600); + Serial1.print("AT"); + delay(400); + Serial1.print("AT+ROLE0"); // set the role as peripheral. + delay(400); + Serial1.print("AT+NAMEGroup7IOT"); // set the name as Ratata. + Serial1.flush(); +} + +void blueToothCall(){ + //BLUETOOTH SECTION + char recvChar; + String WholeCommand = ""; + /*DEBUG START*/ + while(Serial1.available()){ + recvChar = Serial1.read(); + Serial.print(recvChar); + } + while(Serial.available()){ + WholeCommand = SerialString(); + Serial1.print(WholeCommand); + delay(400); + } +} + + + +// play tone +void playTone(int tone, int duration) { + for (long i = 0; i < duration *1000L; i += tone *2){ + digitalWrite(BUZZER_PIN, HIGH); + delayMicroseconds(tone); + digitalWrite(BUZZER_PIN, LOW); + delayMicroseconds(tone); + } +} + +void playNote(char note, int duration){ + char names[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'A'}; + int tones[] = {2109, 1879, 1774, 1580, 1408, 1329, 1184, 1054}; + + + + for (int i = 0; i < 8; i++){ + if (names[i] == note){ + playTone(tones[i], duration); + } + } +} -- GitLab