Trying to implement serial data transmission using two digital pins other than rx and tx between arduino and esp32 - arduino

I am developing a car robot for that I am using arduino and motor driving shield. I am trying to send commands from esp32 to arduino(as I am getting commands to esp32 from internet).
commands like forward, backward, right, left, autonomous, manual.
as rx and tx are used by motor driving shield, I decided to transfer data from esp32 to arduino through custom serial communication(by sending pulses for certain amount of durations)
arduino code:
void setup() {
Serial.begin(9600);
Serial.println("started");
pinMode(cmd_pin, INPUT);
pinMode(data_validator, OUTPUT);
digitalWrite(data_validator, HIGH);
}
String check(){
//String s = "";
int serial_data = digitalRead(cmd_pin);
if(serial_data==LOW){
delay(10);
digitalWrite(data_validator, LOW);
unsigned long high_dur = pulseIn(cmd_pin, HIGH);
unsigned long low_dur = pulseIn(cmd_pin, LOW);
Serial.println("HIGH ");
Serial.println(high_dur);
Serial.println("low ");
Serial.println(low_dur);
if(high_dur<=5000 && low_dur>24000){
automatic = false;
return "Forward";
}
else if(high_dur<=10000 && low_dur>19000){
automatic = false;
return "Backward";
}
else if(high_dur<=15000 && low_dur>14000){
automatic = false;
return "Turing right";
}
else if(high_dur<=20000 && low_dur>9000){
automatic = false;
return "Turing left";
}
else if(high_dur<=25000 && low_dur>4000){
automatic = true;
return "autonomous";
}
else if(high_dur<=30000 && low_dur>1900){
automatic = false;
return "manual";
}
digitalWrite(data_validator, HIGH);
}
}
void loop(){
command = check();
//ruuning command
Serial.println(command);
// if (command=="autonomous")
// auton();
// else if (command=="manual")
// moveStop();
// else if (command=="Turing left")
// turnLeft();
// else if (command=="Turing right")
// turnRight();
// else if (command=="Forward")
// moveForward();
// else if (command=="Backward")
// moveBackward();
// else{
// if (automatic)
// auton();
//}
}
information is transferred through cmd_pin,.
when low occurs in cmd_pin Arduino set data validator to low denotes the esp32 to send data
high_duration and low_duration are measure and decision is taken based on the duration.
esp32 code:
void manual(){
serial_busy = true;
digitalWrite(cmd_pin, LOW);
while (digitalRead(data_validator)!=LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(30);
digitalWrite(cmd_pin, LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(1);
digitalWrite(cmd_pin,LOW);
delay(2);
digitalWrite(cmd_pin,HIGH);
serial_busy = false;
delay(10);
}
void forward(){
serial_busy = true;
Serial.println("Forward");
digitalWrite(cmd_pin, LOW);
while (digitalRead(data_validator)!=LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(5);
digitalWrite(cmd_pin, LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(1);
digitalWrite(cmd_pin,LOW);
delay(25);
digitalWrite(cmd_pin,HIGH);
serial_busy = false;
delay(10);
}
void backward(){
serial_busy = true;
Serial.println("Backward");
digitalWrite(cmd_pin, LOW);
while (digitalRead(data_validator)!=LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(10);
digitalWrite(cmd_pin, LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(1);
digitalWrite(cmd_pin,LOW);
delay(20);
digitalWrite(cmd_pin,HIGH);
serial_busy = false;
delay(10);
}
void right(){
serial_busy = true;
Serial.println("Right");
digitalWrite(cmd_pin, LOW);
while (digitalRead(data_validator)!=LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(15);
digitalWrite(cmd_pin, LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(1);
digitalWrite(cmd_pin,LOW);
delay(15);
digitalWrite(cmd_pin,HIGH);
serial_busy = false;
delay(10);
}
void left(){
serial_busy = true;
Serial.println("Left");
digitalWrite(cmd_pin, LOW);
while (digitalRead(data_validator)!=LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(20);
digitalWrite(cmd_pin, LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(1);
digitalWrite(cmd_pin,LOW);
delay(10);
digitalWrite(cmd_pin,HIGH);
serial_busy = false;
delay(10);
}
void auton(){
serial_busy = true;
Serial.println("going autonomusly");
digitalWrite(cmd_pin, LOW);
while (digitalRead(data_validator)!=LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(25);
digitalWrite(cmd_pin, LOW);
delay(1);
digitalWrite(cmd_pin,HIGH);
delay(1);
digitalWrite(cmd_pin,LOW);
delay(5);
digitalWrite(cmd_pin,HIGH);
serial_busy = false;
delay(10);
}
void setup(){
Serial.begin(115200);
pinMode(cmd_pin, OUTPUT);
pinMode(data_validator, INPUT);
digitalWrite(cmd_pin, HIGH);
}
void loop(){
//call the functions left or right ..
}
I am calling the functions left,right,.... based on my websocket output(Not Included here)
where I was wrong, or is there any other methods available?
constrains: Pins available in arduino : 2,13, A3,A4 ,A5 (all other pins are used by other components)
I am expecting the best way to transfer data between arduino and esp32 loss-less transmission
using the availble pins only.

There is a software serial library available that you can use to enable serial communication over any GPIO pins.
Arduino Software Serial

Related

getting a error: 'class WiFiServer' has no member named 'arg'

im getting a error like
error: 'class WiFiServer' has no member named 'arg'
my code
#define ENA 14 // Enable/speed motors Right GPIO14(D5)
#define ENB 12 // Enable/speed motors Left GPIO12(D6)
#define IN_1 13 // L298N in1 motors Rightx GPIO13(D7)
#define IN_2 15 // L298N in2 motors Right GPIO15(D8)
#define IN_3 2 // L298N in3 motors Left GPIO2(D4)
#define IN_4 0 // L298N in4 motors Left GPIO0(D3)
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
String command; //String to store app command state.
int speedCar = 100; // 400 - 1023.
const char* ssid = "your wifi";
const char* password = "your password";
WiFiServer server(80);
IPAddress local_IP(192, 168, 1, 184);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
void setup() {
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
Serial.begin(115200);
// Connecting WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected");
server.begin();
Serial.println("Server Connected");
Serial.print("Go to this link: https://");
Serial.println(WiFi.localIP());
}
void goAhead(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goBack(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goRight(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goLeft(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goAheadRight(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goAheadLeft(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}
void goBackRight(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void goBackLeft(){
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void stopRobot(){
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("Waiting for new client");
while (!client.available()) {
delay(1);
}
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
if (request.indexOf("/State=F")) command = "F";
else if (request.indexOf("/State=B")) command = "B";
else if (request.indexOf("/State=L")) command = "L";
else if (request.indexOf("/State=R")) command = "R";
else if (request.indexOf("/State=I")) command = "I";
else if (request.indexOf("/State=G")) command = "G";
else if (request.indexOf("/State=J")) command = "J";
else if (request.indexOf("/State=H")) command = "H";
else if (request.indexOf("/State=0")) command = "0";
else if (request.indexOf("/State=1")) command = "1";
else if (request.indexOf("/State=2")) command = "2";
else if (request.indexOf("/State=3")) command = "3";
else if (request.indexOf("/State=4")) command = "4";
else if (request.indexOf("/State=5")) command = "5";
else if (request.indexOf("/State=6")) command = "6";
else if (request.indexOf("/State=7")) command = "7";
else if (request.indexOf("/State=8")) command = "8";
else if (request.indexOf("/State=9")) command = "9";
else if (request.indexOf("/State=S")) command = "S";
else Serial.println("Wrong command");
command = server.arg("State");
if (command == "F") goAhead();
else if (command == "B") goBack();
else if (command == "L") goLeft();
else if (command == "R") goRight();
else if (command == "I") goAheadRight();
else if (command == "G") goAheadLeft();
else if (command == "J") goBackRight();
else if (command == "H") goBackLeft();
else if (command == "0") speedCar = 100;
else if (command == "1") speedCar = 117;
else if (command == "2") speedCar = 135;
else if (command == "3") speedCar = 153;
else if (command == "4") speedCar = 170;
else if (command == "5") speedCar = 190;
else if (command == "6") speedCar = 205;
else if (command == "7") speedCar = 223;
else if (command == "8") speedCar = 240;
else if (command == "9") speedCar = 255;
else if (command == "S") stopRobot();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<br><button>ahead left</button>");
client.println("<button>ahead</button>");
client.println("<button>ahead right</button><br>");
client.println("<br><button>left</button>");
client.println("<button>stop</button>");
client.println("<button>right</button><br>");
client.println("<br><button>back left</button>");
client.println("<button>back</button>");
client.println("<button>back right</button><br>");
client.println("<button>Speed 0</button>");
client.println("<button>Speed 1</button>");
client.println("<button>Speed 2</button>");
client.println("<button>Speed 3</button>");
client.println("<button>Speed 4</button>");
client.println("<button>Speed 5</button>");
client.println("<button>Speed 6</button>");
client.println("<button>Speed 7</button>");
client.println("<button>Speed 8</button>");
client.println("<button>Speed 9</button>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
}
actually im making a web controlled car where it can be controlled with any device.
thanks in advance.
You've confused WiFiServer for ESP8266WebServer.
WiFiServer simply listens for raw TCP connections and acts as a server for them. It is not and never has been a web server. TCP connections don't have arguments passed to them, so there is no arg method.
You need to rewrite your code to use ESP8266WebServer - you included the header file for it but never used it.
There are plenty of examples on how to use it in its repository.

Serial Print once only

I am a ON/OFF program in which if I turn On it will serial print to serial monitor 1 "ON" only but still looping and turn On the outputs and if turn Off it will serial print 1 "OFF" only but still looping and turn Off the outputs .
Here's my code:
int pbuttonPin = 7;// push button
int fan = 8;
int water = 9;
int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status
void setup() {
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(fan, OUTPUT);
pinMode(water, OUTPUT);
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
}
void loop() {
val = digitalRead(pbuttonPin);// read the push button value
if(val == HIGH && lightON == LOW){
pushed = 1-pushed;
delay(100);
}
lightON = val;
if(pushed == LOW){
Serial.print("ON\n");
Serial.println();
digitalWrite(fan, LOW);
digitalWrite(water, LOW);
delay(100);
}
else if(pushed == HIGH) {
Serial.print("OFF\n");
Serial.println();
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
delay(100);
}
}
I guess you want something like this but i am not sure
bool buttonState = false, buttonStateBefore = false;
buttonState = !digitalRead(buttonPin); //needs to be inverted because INPUT_PULLUP
if(buttonState > buttonStateBefore) doStuff(); //Serial print and turn on/off ligths
buttonStateBefore = buttonState;
Now doStuff() is only called once when the button is pressed.
Maybe this works for you
Add this at last of the code Sting previousval = val at the end of the code and put a if statement which checks if the previous value has changed or not. If it is, then run the code and put this part which contains the print statement in it . basically this will be your code
if ( previousval =! val){
if(pushed == LOW){
Serial.print("ON\n");
Serial.println();
digitalWrite(fan, LOW);
digitalWrite(water, LOW);
delay(100);
}
else if(pushed == HIGH) {
Serial.print("OFF\n");
Serial.println();
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
delay(100);
previousval = val
}
}

Sending an array from one Nodemcu to another Nodemcu?

I am working on a gesture control car project and I find everyone on the internet using RF modules or Bluetooth modules to do it, I had two NodeMCU lying around so I thought why not use them and bring a change. But I am finding difficulty in sending the accelerometer values from one Nodemcu to another, For connecting the two Nodemcu I took help from here
https://www.instructables.com/id/WiFi-Communication-Between-Two-ESP8266-Based-MCU-T/
So instead of a string (which they use), I tried sending an array of two variables, but I fail to send/receive it from the transmitter to the receiver. I have spent a lot of time on it, but I think I need help.
I now know ( just this morning ) that Nodemcu has only one analog pin, I have ordered for a 74HC4051 IC. So I haven't changed the code for that, so please bear that in mind. Also, I have used the xAxis only now just to test if the data is being sent, but it is not.
If there is any other easier way of sending the two variable values, please do suggest.
This is the actual project, I asked the author for help, but no reply yet.
https://www.youtube.com/watch?v=svJwmjplm4c
I have given below the edited code that I have now
Transmitter code
#include <SPI.h>
#include <ESP8266WiFi.h>
int xAxis = A0;
int yAxis = D2;
int data[1];
int x;
int ledPin = D8;
char ssid[] = "********"; // SSID
char pass[] = "********"; // password I removed them while posting the question
WiFiServer server(80);
IPAddress ip(192, 168,1, 80); // IP address of the server
IPAddress gateway(192,168,1,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup() {
Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
server.begin(); // starts the server
Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // some parameters from the network
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());
Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
pinMode(ledPin, OUTPUT);
}
void loop () {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
client.flush();
Serial.println("Data send");
x= analogRead(xAxis);
data[0]= analogRead(xAxis);
client.println(data[x,yAxis]);
Serial.println(x);
Serial.println(data[0]); //This appears on the screen only if i have the line data[0]= analogRead(xAxis);
digitalWrite(ledPin, HIGH);
}
client.stop(); // tarminates the connection with the client
}delay(2000);
}
Receiver Code
#include <SPI.h>
#include <ESP8266WiFi.h>
int ENA = D1;
int ENB = D2;
int MotorA1 = D3;
int MotorA2 = D4;
int MotorB1 = D5;
int MotorB2 = D6;
int data[1];
int xAxis;
int yAxis;
int ledPin = D8;
char ssid[] = "*****";
char pass[] = "*****";
unsigned long askTimer = 0;
IPAddress server(192,168,1,80); // the fix IP address of the server
WiFiClient client;
void setup() {
Serial.begin(115200); // only for debug
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
/* Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // Network parameters
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());*/
pinMode(ledPin, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(MotorA1, OUTPUT);
pinMode(MotorA2, OUTPUT);
pinMode(MotorB1, OUTPUT);
pinMode(MotorB2, OUTPUT);
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
void loop () {
client.connect(server, 80); // Connection to the server
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
data[xAxis,yAxis] = client.read(); // receives the answer from the sever
Serial.println(" Received from Server ");
client.flush();
digitalWrite(ledPin, HIGH);
Serial.println(xAxis);
if(yAxis > 400) {
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else if(yAxis < 320) {
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
} else if(xAxis < 320){
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else if(xAxis > 400){
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
analogWrite(ENA, 150);
analogWrite(ENB, 150);
}else {
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}
delay(2000); // client will trigger the communication after two seconds
}

how to control door using SIM900a

I need help I'm doing project about opening and closing the door using SIM900a.. it says that no errors but it doesn't work !! I used AT commands and it supposes to send notification to the user using sms when theres knocking, motion at home, high temperature and when theirs smoke please help please please
#include <SoftwareSerial.h>
#include "pins_arduino.h"
#include <String.h>
SoftwareSerial SIM900(7, 8); //tx & rx pins
String msg = String("");
char inchar;
int x = 0;
int y = 0;
float z =0;
int PIR_sensor = 12;
int door_lock = 4 ; //close the door
int door_lock1 = 5 ;
int led = 11;
int led1 = 10; //red led
int mic= 6;
int Gas_sensor = A0;
float Tem_sensor = A1;
float temp =0.0;
String textForGAS ;
String textForPIR ;
String textForTAM ;
void setup()
{
Serial.begin(19200);
pinMode(PIR_sensor, INPUT);
pinMode(door_lock, OUTPUT);
pinMode(door_lock1, OUTPUT);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
digitalWrite(door_lock, LOW);
digitalWrite(door_lock1, LOW);
digitalWrite(led, LOW);
digitalWrite(led1, LOW);
// wake up the GSM shield
SIM900.begin(19200);
delay(20000); // give time to log on to network.
SIM900.print("AT+CMGF=1r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0r");
// blurt out contents of new SMS upon receipt to the GSM shield’s serial out
delay(100);
Serial.println("Ready…");
}
void sendSMS(String message)
{
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"+96896089681\"");
delay(100);
SIM900.println(message);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
}
void loop()
{
//If a character comes in from the cellular module…
if (SIM900.available())
{
inchar = SIM900.read();
Serial.println(inchar);
if (inchar == '#')
{
delay(10);
inchar = SIM900.read();
if (inchar == 'a')
{
delay(10);
inchar = SIM900.read();
if (inchar == '0')
{
digitalWrite(door_lock, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led, LOW);
}
else if (inchar == '1')
{
digitalWrite(door_lock1, HIGH);
digitalWrite(led, HIGH);
digitalWrite(led1, LOW);
}
delay(10);
inchar = SIM900.read();
if (inchar == 'b')
{
inchar = SIM900.read();
if (inchar == '0')
{
digitalWrite(PIR_sensor, LOW);
}
else if (inchar == '1')
{
digitalWrite(PIR_sensor, HIGH);
}
}
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
Gas_sensor = (analogRead(A0));
PIR_sensor = (digitalRead(12));
Tem_sensor = (analogRead(A1));
temp = Tem_sensor * 0.48828125;
//program for GAS sensor
if (temp > 70.0)
{
textForTAM = "Alarm ! The degree of Tamperture is :n ";
textForTAM.concat(temp);
textForTAM = textForTAM +"C";
Serial.println(textForTAM);
sendSMS(textForGAS);
delay(1000);
do {
z = (analogRead(A0));
}
while (z >= 141.312);
}
//program for GAS sensor
if (Gas_sensor > 500)
{
textForGAS = "Alarm ! there is Gas by rate :n ";
textForGAS.concat(Gas_sensor);
Serial.println(textForGAS);
sendSMS(textForGAS);
delay(1000);
}
//program for PIR sensor
if (digitalRead(12) == HIGH)
{
textForPIR = "Warnning ! n theres's a motion ";
textForPIR.concat(PIR_sensor);
Serial.println(textForPIR);
sendSMS(textForPIR);
delay(1000);
}
}
thank you

Communicating with Arduino Serial

I am trying to communicate with my Arduino over the USB port by using Serial:
int previous;
int current = 0;
void turnOn(int pinNumber){
previous = current;
current = pinNumber;
if(previous!=0){
digitalWrite(previous, LOW);
digitalWrite(current, HIGH);
}else{
digitalWrite(current, HIGH);
}
}
void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
Serial.write(1);
}
void loop(){
delay(1);
if(Serial.available()>0){
switch(Serial.read()){
case 0:
turnOn(8);
break;
case 1:
turnOn(9);
break;
case 2:
turnOn(10);
break;
default:
Serial.println(Serial.read());
}
}
}
I am trying so that if I send a 0 the rightmost LED will light up, if I send 1, the middle one will and if I send a 2 the leftmost will. However when I send 0,1 or anything else it prints a -1 meaning the default switch has been triggered. How do I fix it?
Try this...
void loop(){
if (Serial.available()) {
char input = Serial.read();
if(input == '0'){
turnOn(8);
}else if(input == '1'){
turnOn(9);
}else if(input == '2'){
turnOn(10);
}
}
}
Tell me if it works or not then we can proceed :)

Resources