I want to light a LED to this code with the "2" ArduDroid app (Hazim Bitar)
#include <Servo.h>
#include <NewPing.h>
/*
PROJECT: ArduDroid
PROGRAMMER: Hazim Bitar (techbitar at gmail dot com)
DATE: Oct 31, 2013
FILE: ardudroid.ino
LICENSE: Public domain
*/
#define START_CMD_CHAR '*'
#define END_CMD_CHAR '#'
#define DIV_CMD_CHAR '|'
#define CMD_DIGITALWRITE 10
#define CMD_ANALOGWRITE 11
#define CMD_TEXT 12
#define CMD_READ_ARDUDROID 13
#define MAX_COMMAND 20 // max command number code. used for error checking.
#define MIN_COMMAND 10 // minimum command number code. used for error checking.
#define IN_STRING_LENGHT 40
#define MAX_ANALOGWRITE 255
#define PIN_HIGH 3
#define PIN_LOW 2
String inText;
//motor 1
int motorOneA = 2;
int motorOneB = 4;
int speedPinA = 5; //Must be a PWM pin for speed control
// Motor 2
int motorTwoA = 7;
int motorTwoB = 8;
int speedPinB = 6; //Must be a PWM pin for speed control
//Servo Stuff
Servo scanServo;
Servo servo1;
Servo servo2;
Servo clawRotate;
Servo claw;
int servoPosition = 1500;
int servoIncrement = 200;
//Led pins
int obstacleDetectedLED = 10;
int obstacleNotDetectedLED = 9;
void setup() {
Serial.begin(9600);
Serial.flush();
//Attach Servo objects to corresponding pin
scanServo.attach(12);
servo1.attach(15);
servo2.attach(14);
clawRotate.attach(16);
claw.attach(17);
//Set all Servos to Center position
scanServo.writeMicroseconds(1500);
scanServo.writeMicroseconds(1500);
servo1.writeMicroseconds(1500);
servo2.writeMicroseconds(1500);
clawRotate.writeMicroseconds(1500);
claw.writeMicroseconds(1500);
//Set LED pins to output
pinMode(obstacleDetectedLED, OUTPUT);
pinMode(obstacleNotDetectedLED, OUTPUT);
}
void loop()
{
Serial.flush();
int ard_command = 0;
int pin_num = 0;
int pin_value = 0;
char get_char = ' '; //read serial
// wait for incoming data
if (Serial.available() < 1) return; // if serial empty, return to loop().
// parse incoming command start flag
get_char = Serial.read();
if (get_char != START_CMD_CHAR) return; // if no command start flag, return to loop().
// parse incoming command type
ard_command = Serial.parseInt(); // read the command
// parse incoming pin# and value
pin_num = Serial.parseInt(); // read the pin
pin_value = Serial.parseInt(); // read the value
// 1) GET TEXT COMMAND FROM ARDUDROID
if (ard_command == CMD_TEXT){
inText =""; //clears variable for new input
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
delay(5);
if (c == END_CMD_CHAR) { // if we the complete string has been read
break;
}
else {
if (c != DIV_CMD_CHAR) {
inText += c;
delay(5);
}
}
}
}
// 2) GET digitalWrite DATA FROM ARDUDROID
if (ard_command == CMD_DIGITALWRITE){
if (pin_value == PIN_LOW) pin_value = LOW;
else if (pin_value == PIN_HIGH) pin_value = HIGH;
else return; // error in pin value. return.
set_digitalwrite( pin_num, pin_value); // Uncomment this function if you wish to use
return; // return from start of loop()
}
// 3) GET analogWrite DATA FROM ARDUDROID
if (ard_command == CMD_ANALOGWRITE) {
pin_value=map(pin_value, 0, 255, 500, 2400); // Map MIN and MAX values for Pin_value varialbe to MIN and MAX servo positions
if (pin_num==11){ //Convert pin_num from Ardudroid to Actual pin of Servos
pin_num==15;
servo1.writeMicroseconds(pin_value);
}
if (pin_num==10) {
pin_num==14;
servo2.writeMicroseconds(pin_value);
}
if (pin_num==9) {
pin_num==16;
clawRotate.writeMicroseconds(pin_value);
}
if(pin_num==6) {
pin_num==17;
claw.writeMicroseconds(pin_value);
}
// add your code here
return; // Done. return to loop();
}
// 4) SEND DATA TO ARDUDROID
if (ard_command == CMD_READ_ARDUDROID) {
// char send_to_android[] = "Place your text here." ;
// Serial.println(send_to_android); // Example: Sending text
Serial.print(" Analog 0 = ");
Serial.println(analogRead(A0)); // Example: Read and send Analog pin value to Arduino
return; // Done. return to loop();
}
}
// 2a) select the requested pin# for DigitalWrite action
void set_digitalwrite(int pin_num, int pin_value)
{
switch (pin_num) {
case 13:
//Forward
digitalWrite(obstacleNotDetectedLED, HIGH);
delay(250);
digitalWrite(obstacleNotDetectedLED, LOW);
analogWrite(speedPinA, 255);//Sets speed variable via PWM
digitalWrite(motorOneA, LOW);
digitalWrite(motorOneB, HIGH);
analogWrite(speedPinB, 255);//Sets speed variable PWM
digitalWrite(motorTwoA, HIGH);
digitalWrite(motorTwoB, LOW);
Serial.println("Forward"); // Prints out “Motor 1 Forward” on the serial monitor
Serial.println(" "); // Creates a blank line printed on the serial monitor
break;
case 12:
//Backward
digitalWrite(obstacleNotDetectedLED, HIGH);
delay(250);
digitalWrite(obstacleNotDetectedLED, LOW);
analogWrite(speedPinA, 255);
digitalWrite(motorOneA, HIGH);
digitalWrite(motorOneB, LOW);
analogWrite(speedPinB, 255);
digitalWrite(motorTwoA, LOW);
digitalWrite(motorTwoB, HIGH);
Serial.println("Backward");
Serial.println(" ");
break;
case 11:
//Right
digitalWrite(obstacleNotDetectedLED, HIGH);
delay(250);
digitalWrite(obstacleNotDetectedLED, LOW);
analogWrite(speedPinA, 255);
digitalWrite(motorOneA, LOW);
digitalWrite(motorOneB, HIGH);
analogWrite(speedPinB, 255);
digitalWrite(motorTwoA, LOW);
digitalWrite(motorTwoB, HIGH);
Serial.println("Right");
Serial.println(" ");
break;
break;
case 10:
//Left
digitalWrite(obstacleNotDetectedLED, HIGH);
delay(250);
digitalWrite(obstacleNotDetectedLED, LOW);
analogWrite(speedPinA, 255);
digitalWrite(motorOneA, HIGH);
digitalWrite(motorOneB, LOW);
analogWrite(speedPinB, 255);
digitalWrite(motorTwoA, HIGH);
digitalWrite(motorTwoB, LOW);
Serial.println("Left");
Serial.println(" ");
break;
case 9:
//Brakes
digitalWrite(obstacleNotDetectedLED, HIGH);
delay(250);
digitalWrite(obstacleNotDetectedLED, LOW);
analogWrite(speedPinA, 0);
digitalWrite(motorOneA, LOW);
digitalWrite(motorOneB, LOW);
analogWrite(speedPinB, 0);
digitalWrite(motorTwoA, LOW);
digitalWrite(motorTwoB, LOW);
break;
case 8:
//increment the positions of the servo by the value of servo increment
pinMode(8, OUTPUT);
servoPosition += servoIncrement;
servo1.writeMicroseconds(servoPosition);
delay(10);
break;
case 7:
pinMode(7, OUTPUT);
servoPosition -= servoIncrement;
servo1.writeMicroseconds(servoPosition);
delay(10);
break;
case 6:
pinMode(6, OUTPUT);
servoPosition +=servoIncrement;
servo2.writeMicroseconds(servoPosition);
delay(10);
break;
case 5:
pinMode(5, OUTPUT);
servoPosition -= servoIncrement;
servo2.writeMicroseconds(servoPosition);
delay(10);
break;
case 4:
pinMode(4, OUTPUT);
servoPosition += servoIncrement;
clawRotate.writeMicroseconds(servoPosition);
delay(10);
break;
case 3:
pinMode(3, OUTPUT);
servoPosition -= servoIncrement;
clawRotate.writeMicroseconds(servoPosition);
delay(10);
break;
case 2:
pinMode(2, OUTPUT); //not used
digitalWrite(2, pin_value);
break;
// default:
// if nothing else matches, do the default
// default is optional
}
}