Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this code (below), and I\'m trying to get it to work correctly. I need th

ID: 2249669 • Letter: I

Question

I have this code (below), and I'm trying to get it to work correctly. I need the LED in pin 13 to always be on, and the LED in pin 12 to toggle based on movement. When the motion sensor senses movement, it needs to turn on pin 12 for 10 seconds. Can you fix/create code for this? Vin is the battery input (12V), which pin is needed to power the motion sensor. PS: We need the output of the pins to be 12V as well.

int output1 = 13; // output1 connected to pin 13

int output2 = 12; // output2 connected to pin 13

int inputPin = 2; // choose the input pin (for PIR sensor)

int pirState = LOW; // we start, assuming no motion detected

int val = 0; // variable for reading the pin status

void setup() {

pinMode(output1, OUTPUT); // declare output1 & output2 as output

pinMode(output2, OUTPUT);

pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);

}

  

void loop(){

digitalWrite(output1, HIGH); // Always high we didn’t check any condition

val = digitalRead(inputPin); // read input value from motion sensor

if (val == HIGH) { // check if the input is HIGH(Motion Detected

digitalWrite(output2, HIGH); // turn LED ON

if (pirState == LOW) {

// we have just turned on

Serial.println("Motion detected!");

// We only want to print on the output change, not state

pirState = HIGH;

}

} else {

digitalWrite(output2, LOW); // turn OFF output when motion ended

if (pirState == HIGH){

// we have just turned of

Serial.println("Motion ended!");

// We only want to print on the output change, not state

pirState = LOW;

}

}

}

Explanation / Answer

load this code below to your Arduino. As it is, you can control pins 11, 12, and 13. If you would like to control more pins simply add the initial pinMode and digitalWrite lines for that pin.

#include <SoftwareSerial.h>

#define DEBUG true

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
  
pinMode(11,OUTPUT);
digitalWrite(11,LOW);
  
pinMode(12,OUTPUT);
digitalWrite(12,LOW);
  
pinMode(13,OUTPUT);
digitalWrite(13,LOW);

sendData("AT+RST ",2000,DEBUG); // reset module
sendData("AT+CWMODE=2 ",1000,DEBUG); // configure as access point
sendData("AT+CIFSR ",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1 ",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80 ",1000,DEBUG); // turn on server on port 80
}

void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{

  
if(esp8266.find("+IPD,"))
{
delay(1000); // wait for the serial buffer to fill up (read all the serial data)
// get the connection id so that we can then disconnect
int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
  
esp8266.find("pin="); // advance cursor to "pin="

int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number

digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin   

// make close command
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+=" ";

sendData(closeCommand,1000,DEBUG); // close connection
}
}
}

/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
  
esp8266.print(command); // send the read character to the esp8266
  
long int time = millis();
  
while( (time+timeout) > millis())
{
while(esp8266.available())
{
  
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}  
}
  
if(debug)
{
Serial.print(response);
}
  
return response;
}