Saturday, August 17, 2019

IOT Based Accelerometer Sensor controlled Robot using Blynk App



CODE

#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "Your Blynk Auth ID ";
char ssid[] = "Your WI-FI Name";
char pass[] = "Your WI-FI Password";
const int ml1 = D1;
const int ml2 = D2;
const int mr1 = D3;
const int mr2 = D4;
void setup()
{
  pinMode(ml1, OUTPUT);
  pinMode(ml2,OUTPUT);
  pinMode(mr1, OUTPUT);
  pinMode(mr2,OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
BLYNK_WRITE(V1)
{
int x = param[0].asFloat(); //acceleration force applied to axis x
int y = param[1].asFloat(); //acceleration force applied to axis y
int z = param[2].asFloat();//acceleration force applied to axis y
/*x = map(x, -10, 10, 255, -255);
y = map(y, -10, 10, 255, -255);
z = map(z, -10, 10, 255, -255);*/
Serial.print("x axis:");
Serial.println(x);
Serial.print("y axis:");
Serial.println(y);
Serial.print("z axis:");
Serial.println(z);
if(y==0&&x==0)
{
digitalWrite(ml1, HIGH);//stop
digitalWrite(ml2, HIGH);
digitalWrite(mr1, HIGH);
digitalWrite(mr2, HIGH);
}
else if(y<0)
{
 digitalWrite(ml1, HIGH);//forward
digitalWrite(ml2, LOW);
digitalWrite(mr1, HIGH);
digitalWrite(mr2, LOW);
}
else if(z<6)
{
 digitalWrite(ml1, LOW);//reverse
digitalWrite(ml2, HIGH);
digitalWrite(mr1, LOW);
digitalWrite(mr2, HIGH);
}
else if(x>1)
{
 digitalWrite(ml1,HIGH);
digitalWrite(ml2, LOW);//right
digitalWrite(mr1, LOW);
digitalWrite(mr2, HIGH); 
}
else if(x<0)
{
digitalWrite(ml1,LOW);
digitalWrite(ml2, HIGH);
digitalWrite(mr1, HIGH);//left
digitalWrite(mr2, LOW);
}
else
{

}
}

2 comments:

  1. Can u pls explain it to me why did u take z<6

    ReplyDelete
  2. Sir i am really confused about the code..can u pls make a video explaining the code step by step , i will really be grateful to u

    ReplyDelete

CIRCUITS WITH PROJECTS SOURCE CODES

Flex Sensor And Accelerometer-based Gesture Controlled Robot With Robot...