Hi my first video of my robot i am using Arduino (2009) and wifly shield
I used Arduino (2009), Wifly shield,h-bridge,shift register and a lcd.This is my first attemt to make somthing beyond blink led.
Hi my first video of my robot i am using Arduino (2009) and wifly shield
I used Arduino (2009), Wifly shield,h-bridge,shift register and a lcd.This is my first attemt to make somthing beyond blink led.
Oh man, what a project!! Very nice!!!
Looking good, but I think you want it to only move a defined amount of time then automatically stop, instead of having it moving until it is told to stop.
This is what I got for my bot
//** Settings **//
//Volts needed to be functional
#define nV 8.5
//PWM amount output to each motor (0-255)
#define lM 140
#define rM 140
//** Analog pins **//
//Battery info
#define bV 14 //Battery voltage
#define bA 13 //Battery current
//** Digital pins **//
//H-Bridge pins
#define I1 41
#define I2 40
#define I3 39
#define I4 38
#define EB 4 //PWM
#define EA 3 //PWM
//Proximity sensor pins
#define pFl 37 //Front left
#define pFr 36 //Front right
#define pBL 35 //Behind left
#define pBr 34 //Behind right
//Ping sensor pins
#define fP 33 //Front
#define bP 32 //Behind
//Relays
#define kS 31 //Kill switch
#define lS 30 //Light switch
//Battery info
#defind pAp 48 //Current sensor, positive
#defind pAn 49 //Current sensor, negative
//Input pins
int iPins[] = {
pFr, pFl};
//Output pins
int oPins[] = {
kS, lS, I1, I2, I3, I4, pAp, pAn};
//Used to make the tank deny all movements
boolean online = true;
//Used for the proximity sensors, to be able to move away from the obstacle
int lastMove = 0; //1 = forwards - 2 = backwards
//Millis to calculate when to call the stopS() function
unsigned long stopMove = 0;
//Milliseconds it should move
int stopTime = 0;
//Milliseconds from when cV was over nV
unsigned long vD = 0;
//Current Voltage
double cV = 0;
//Current Amps
double cA = 0;
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {
192,168,1,15}; // IP address of WiShield
unsigned char gateway_ip[] = {
192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {
255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {
"Captain Slow"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {
"xxxxxxxxxxxxxxxx"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
// This is our page serving function that generates web pages and handles requests
boolean sendMyPage(char* URL)
{
// Use WiServer's print and println functions to write out the page content
WiServer.print(cV);
WiServer.print("|");
WiServer.print(cA);
if (strcmp(URL, "/F1") == 0)
{
stopTime = 750;
stopMove = millis();
moveF();
}
if (strcmp(URL, "/F2") == 0)
{
stopTime = 375;
stopMove = millis();
moveF();
}
else if (strcmp(URL, "/B1") == 0)
{
stopTime = 750;
stopMove = millis();
moveB();
}
else if (strcmp(URL, "/B2") == 0)
{
stopTime = 375;
stopMove = millis();
moveB();
}
else if (strcmp(URL, "/L3") == 0)
{
stopTime = 300;
stopMove = millis();
moveL();
}
else if (strcmp(URL, "/L2") == 0)
{
stopTime = 200;
stopMove = millis();
moveL();
}
else if (strcmp(URL, "/L1") == 0)
{
stopTime = 100;
stopMove = millis();
moveL();
}
else if (strcmp(URL, "/R3") == 0)
{
stopTime = 300;
stopMove = millis();
moveR();
}
else if (strcmp(URL, "/R2") == 0)
{
stopTime = 200;
stopMove = millis();
moveR();
}
else if (strcmp(URL, "/R1") == 0)
{
stopTime = 100;
stopMove = millis();
moveR();
}
return true;
}
void moveF() //Forwards
{
//if (digitalRead(pFl) == HIGH && digitalRead(pFr) == HIGH)
{
lastMove = 1;
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
}
void moveB() //Backwards
{
lastMove = 2;
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
void moveL() //Left
{
//if (digitalRead(pFl) == HIGH)
{
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
}
void moveR() //Right
{
//if (digitalRead(pFr) == HIGH)
{
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
}
void moveS() //Stop
{
lastMove = 0;
stopMove = 0;
analogWrite(EA, 0);
analogWrite(EB, 0);
digitalWrite(I1, HIGH);
digitalWrite(I2, HIGH);
digitalWrite(I3, HIGH);
digitalWrite(I4, HIGH);
}
void moveES() //Emergency Stop
{
moveB();
delay(50);
moveF();
delay(50);
moveS();
}
void cutPower() //Pull the latching relay to turn everything off
{
digitalWrite(kS, HIGH);
delay(100);
digitalWrite(kS, LOW);
}
double pingF() //Get CM to obstacle in front of the tank
{
long duration;
pinMode(fP, OUTPUT);
digitalWrite(fP, LOW);
delayMicroseconds(2);
digitalWrite(fP, HIGH);
delayMicroseconds(15);
digitalWrite(fP, LOW);
delayMicroseconds(20);
pinMode(fP, INPUT);
duration = pulseIn(fP, HIGH);
return duration / 29.0 / 2.0;
}
double pingB() //Get CM to obstacle in behind the tank
{
long duration;
pinMode(bP, OUTPUT);
digitalWrite(bP, LOW);
delayMicroseconds(2);
digitalWrite(bP, HIGH);
delayMicroseconds(15);
digitalWrite(bP, LOW);
delayMicroseconds(20);
pinMode(bP, INPUT);
duration = pulseIn(bP, HIGH);
return duration / 29.0 / 2.0;
}
//IP of my NAS
uint8 ip[] = {
192,168,1,245};
// Function to print response from the web server
void printData(char* data, int len)
{
}
//Define the request
GETrequest getServer(ip, 80, "192.168.1.245", "");
void setup()
{
digitalWrite(49, LOW); //Negative for current sensor
digitalWrite(48, HIGH); //Positive for current sensor
//Set all output pins
for(int i=0; i < sizeof(oPins) / sizeof(oPins[0]); i++)
{
pinMode(oPins[i], OUTPUT);
}
//Set all input pins
for(int i=0; i < sizeof(iPins) / sizeof(iPins[0]); i++)
{
pinMode(iPins[i], INPUT);
}
//Reset H-Bridge
moveS();
//Prepare WiShield
getServer.setReturnFunc(printData);
WiServer.init(sendMyPage);
}
void loop()
{
cV = map(analogRead(bV), 0, 1023, 0, 1933) / 100.0;
cA = map(analogRead(bA), 512, 1023, 0, 5000) / 100.0;
if (cA < 0.01)
{
cA = 0.01;
}
WiServer.server_task();
if (lastMove == 1 && (digitalRead(pFl) == LOW || digitalRead(pFr) == LOW))
{
//moveES();
}
//if ((millis() - stopMove >= stopTime || cV <= nV) && stopMove != 0)
if ((millis() - stopMove >= stopTime) && stopMove != 0)
{
moveS();
}
//If not online, pull the latching relay to disconnect from battery
if (!online)
{
delay(1000);
cutPower();
delay(250);
return;
}
//If voltage is too low, call the web server to flag the tank as offline
if (cV <= nV)
{
if (millis() - vD >= 2000 && vD != 0) //If the voltage have been below vN for more than 2000 milliseconds, send email and prepare for shut down
{
//getServer.setURL("/arduino/control/call.php?status=nopower");
//getServer.submit();
//Set to offline, so no more commands will be possible
//online = false;
}
//return;
}
else
{
vD = millis();
}
}