Wow. Thanks for the rapid responses. thank you or clearing up some of my confusion (theres alot more where that came from) so here is my... code?... for arduino 1 after youre guys' recomendations.
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library
//control pins
const int LeftMotorForward = 6;
const int LeftMotorBackward = 7;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;
const int ObjectDetected = 10;
const int TurnLeftCommand = 11;
const int TurnRightCommand = 12;
const int MoveForwardCommand = 13;
const int MoveBackwardComand = 14;
//Define Constants
#define trigger_pin_1 8
#define echo_pin_1 8
#define trigger_pin_2 9
#define echo_pin_2 9
#define maximum_distance 250
boolean goesForward = false;
NewPing sonar1(trigger_pin_1, echo_pin_1, maximum_distance);
NewPing sonar2(trigger_pin_2, echo_pin_2, maximum_distance);
// Define Variables
int distance1;
int distance2;
int LED_RED = 10;
int LED_BLUE = 11;
void setup(){
Serial.begin(9600);
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(ObjectDetected, INPUT);
pinMode(TurnLeftCommand, INPUT);
pinMode(TurnRightCommand, INPUT);
pinMode(MoveForwardCommand, INPUT);
pinMode(MoveBackwardComand, INPUT);
}
void loop(){
distance1 = sonar1.ping_cm(250); //measure duration of sesnor 1 ping
delay(250);
distance2 = sonar2.ping_cm(250); //measure duration of sensor 2 ping
delay(50);
if (distance1 <= 35 || distance2 <= 35){
moveStop();
Serial.println('STOP');
delay(1000);
moveBackward();
Serial.println('BACKWARDS');
delay(3000);
moveStop();
Serial.println('STOP');
delay(1000);
if (distance1 > distance2){
turnRight();
Serial.println('RIGHT');
delay(2000);
moveForward();
}
else if(distance1 < distance2){
turnLeft();
Serial.println('LEFT');
delay(2000);
moveForward();
}
else if(distance1 > 35 && distance2 > 35){
moveForward();
Serial.println('FORWARD');
}
}
while(ObjectDetected == HIGH){
if(TurnLeftCommand == HIGH){
turnLeft();
}
if(TurnRightCommand == HIGH){
turnRight();
}
if(MoveForwardCommand == HIGH){
moveForward();
}
if(MoveBackwardComand == HIGH){
moveBackward();
}
}
}
void moveStop(){
analogWrite(RightMotorForward, 0);
analogWrite(LeftMotorForward, 0);
analogWrite(RightMotorBackward, 0);
analogWrite(LeftMotorBackward, 0);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_BLUE, LOW);
}
void moveForward(){
if(!goesForward){
goesForward=true;
analogWrite(LeftMotorForward, 50);
analogWrite(RightMotorForward, 50);
analogWrite(LeftMotorBackward, 0);
analogWrite(RightMotorBackward, 0);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
}
void moveBackward(){
goesForward=false;
analogWrite(LeftMotorBackward, 50);
analogWrite(RightMotorBackward, 50);
analogWrite(LeftMotorForward, 0);
analogWrite(RightMotorForward, 0);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_BLUE, LOW);
}
void turnRight(){
analogWrite(LeftMotorForward, 50);
analogWrite(RightMotorBackward, 50);
analogWrite(LeftMotorBackward, 0);
analogWrite(RightMotorForward, 0);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_BLUE, LOW);
}
void turnLeft(){
analogWrite(LeftMotorBackward, 50);
analogWrite(RightMotorForward, 50);
analogWrite(LeftMotorForward, 0);
analogWrite(RightMotorBackward, 0);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_BLUE, HIGH);
}
heres the code running on arduino 2. hopefully this isn't too amusing lol.
//(150, 130): (x, y): center/ grab location
#include <Pixy2.h>
Pixy2 pixy;
void setup(){
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
Serial.begin(115200);
Serial.print("Starting...\n");
pixy.init();
}
void loop(){
int i;
//get blocks
pixy.ccc.getBlocks();
//If there are detect blocks
if(pixy.ccc.numBlocks){//If object is detected
Serial.print("Detected ");//Print
Serial.println(pixy.ccc.numBlocks);
for (i=0; i<pixy.ccc.numBlocks; i++){
Serial.print(" block ");
Serial.print(i);
Serial.print(": ");
pixy.ccc.blocks[i].print();
}
}
if(pixy.ccc.numBlocks){//if object is detected
digitalWrite(2, HIGH);//pauses autonomous roaming while object is detected
if(!(pixy.ccc.blocks[0].m_x >=130) && (pixy.ccc.blocks[0].m_x <=170)){//if object is not centered on x axis
if(pixy.ccc.blocks[0].m_x <150){//if detected object is left of center x
digitalWrite(3, HIGH);//turn left until centered x
}
else if(pixy.ccc.blocks[0].m_x >150){//if detected object is right of center x
digitalWrite(4, HIGH);//turn right until centered x
}
}
if((pixy.ccc.blocks[0].m_x >=130) && (pixy.ccc.blocks[0].m_x <=170)){//if object is centered on x axis
if(!(pixy.ccc.blocks[0].m_y >=110) && (pixy.ccc.blocks[0].m_y <=150)){//if object is not centered on y axis
if(pixy.ccc.blocks[0].m_y >150){//if object is further away from center y
digitalWrite(5, HIGH);//move forward until centered y
}
else if(pixy.ccc.blocks[0].m_y <150){//if object is closer than center y
digitalWrite(6, HIGH);//move backward until centered y
}
}
if((pixy.ccc.blocks[0].m_x >=130) && (pixy.ccc.blocks[0].m_x <=170) && (pixy.ccc.blocks[0].m_y >=110) && (pixy.ccc.blocks[0].m_y <=150)){//if object is centered
digitalWrite(7, HIGH);//pick up object
delay(30000);
}
else{
digitalWrite(7, LOW);
}
}
}
else{
digitalWrite(2, LOW);//resume autonomous roaming
}
}
Here's the code that operates the robotic arm. This code is tested and works. Except for the arm trying to murder itself upon startup..
#include <VarSpeedServo.h>
VarSpeedServo servo01;
VarSpeedServo servo02;
VarSpeedServo servo03;
VarSpeedServo servo04;
VarSpeedServo servo05;
VarSpeedServo servo06;
VarSpeedServo servo07;
int detectionPin = 4;
int detectionState;
void setup() {
// put your setup code here, to run once:
servo01.attach(5);
servo02.attach(6);
servo03.attach(7);
servo04.attach(8);
servo05.attach(9);
servo06.attach(10);
servo07.attach(11);
servo01.write(90, 30, false);
servo02.write(180, 30, false);
servo03.write(180, 30, false);
servo04.write(135, 30, false);
servo05.write(90, 30, false);
servo06.write(75, 30, false);
servo07.write(75, 30, false);
pinMode(detectionPin, INPUT);
}
void loop() {
// Neutral position, POS A
servo01.write(90, 30, false); // 0-180, increase angle to turn right
servo02.write(180, 30, true); // increase angle to raise arm
servo03.write(180, 30, false); // increase angle to lower arm
servo04.write(135, 30, true); // increase angle to lower arm
servo05.write(90, 30, true); // claw rotation servo
servo06.write(75, 30, false); // 37-75 claw servo 1, increase angle to upen
servo07.write(75, 30, false); // 37-75 claw servo 2, increase angle to open
delay(5000);
detectionState = digitalRead(detectionPin);
if (detectionState == HIGH)
{
// Lower arm and close claw, POS
delay(2000);
servo01.write(90, 30, false); // 0-180, increase angle to turn right
servo02.write(52, 30, false); // increase angle to raise arm
servo03.write(130, 30, true); // increase angle to lower arm
delay(500);
servo04.write(90, 30, true); // increase angle to lower arm
servo05.write(150, 30, false); // claw rotation servo
delay(250);
servo06.write(37, 30, false); // 37-75 claw servo 1, increase angle to open
servo07.write(37, 30, false); // 37-75 claw servo 2, increase angle to open}
delay(5000);
// Return to neutral position
servo01.write(90, 30, false); // 0-180, increase angle to turn right
servo02.write(180, 30, true); // increase angle to raise arm
servo03.write(180, 30, false); // increase angle to lower arm
servo04.write(135, 30, true); // increase angle to lower arm
servo05.write(90, 30, true); // claw rotation servo
servo06.write(75, 30, false); // 37-75 claw servo 1, increase angle to upen
servo07.write(75, 30, false); // 37-75 claw servo 2, increase angle to open
delay(5000);
}
}