Commit 5147deb1 authored by robert.johnson's avatar robert.johnson

Code for IOT Project

parent 3b45426e
#include <Stepper.h>
#include <Wire.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <ThingSpeak.h>
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int x = 0;
int y = 0;
int LightOn = 1;
int LightOff = 0;
const int stepsPerRevolution = 200;
// Create a Stepper object using the specified pins
Stepper myStepper(stepsPerRevolution, 4, 10, 7, 13);
const char ssid[] = "BT-8GAJC3";
const char pass[] = "M4JYKE6C3DrVYH";
const unsigned long channel_id = 1984932;
const char write_api_key[] = "5L5MP1LB128S79N4";
WiFiClient client;
#define trigPin 8
#define echoPin 9
float duration, distance;
void setup() {
myStepper.setSpeed(150); // Set the speed of the stepper motor
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.begin(9600);
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, pass);
Serial.println();
Serial.println("Connected!");
ThingSpeak.begin(client);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Write a pulse to the HC-SR04 Trigger Pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the response from the HC-SR04 Echo Pin
duration = pulseIn(echoPin, HIGH);
// Determine distance from duration
// Use 343 metres per second as speed of sound
distance = (duration / 2) * 0.0343;
// Send results to Serial Monitor
Serial.print("Distance = ");
if (distance >= 400 || distance <= 2) {
Serial.println("Out of range");
}
else {
if (distance > 105bn ) {
y = 0;
ThingSpeak.setField(2, LightOn);
ThingSpeak.writeFields(channel_id, write_api_key);
while(x < 8){
myStepper.step(stepsPerRevolution);
x++;
}
}
// If the sensor value is less than 30, move the stepper motor 5 revolutions backward
else if (distance < 105) {
x = 0;
ThingSpeak.setField(2, LightOff);
ThingSpeak.writeFields(channel_id, write_api_key);
while(y < 5){
myStepper.step(-stepsPerRevolution);
y++;
}
}
}
Serial.print(distance);
Serial.println(" cm");
// Print a message to the LCD.
lcd.clear();
lcd.print(distance);
lcd.print(" cm");
ThingSpeak.setField(1, distance);
ThingSpeak.writeFields(channel_id, write_api_key);
}
\ 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