Pages: 1 2 [3]   Go Down
Author Topic: need some help with code  (Read 3961 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sweet it works now thanks
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

now if i add an ir sensor to the robot will it work like the ping sensor or do i need to add additional code to it? (i'll be using the ir sensor below the ping sensor to catch obstacles below the ping sensor)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so at some point in the future i want to add more sensors to my robot but i am out of pins how would i break this code down so i could use two arduinos via I2C with one running the sensors and the other running just the servos

Code:
#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd screen pins
const int pingPin = 4; //declares the ping pin
const int irPin = 0;  //Sharp infrared sensor pin
const int RForward = 0; //full speed forward
const int RBackward = 180; //full speed reverse
const int LForward = RBackward; //this decides the motor control
const int LBackward = RForward; //this decides the motor control
const int RNeutral = 90; //nuetral for motors
const int LNeutral = 90; //constants for motor speed
const int dangerThresh = 5; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  //this is the "neck" of the robot
Servo leftMotor; //declare left servo motor
Servo rightMotor; //declare right servo motor
long duration; //time it takes to recieve PING))) signal

void setup()
{
  Serial.begin(9600); //allows for serial monitoring
  lcd.begin(16, 2); //turn on the lcd screen
  lcd.print("Hello Jason");
  delay(250);
  lcd.clear();
  lcd.print("Activating KITT");
  delay(250);
  lcd.clear();
  rightMotor.attach(5); //attach left servo motor to proper pin
  leftMotor.attach(6); //attach right servo motor to proper pin
  panMotor.attach(3); //attach pan servo motor to proper pin
  panMotor.write(90); //center the "neck" of the robot
  delay(50);
  lcd.print("KITT Online");
  delay(250);
  lcd.clear();
 
}


void loop()
{
   
  int distanceFwd = ping(); //tells the robot distance forward
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward);
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); //stop forward motion
    panMotor.write(0);
    delay(100);
    rightDistance = ping(); //scan to the right
    delay(100);
    panMotor.write(180);
    delay(100);
    leftDistance = ping(); //scan to the left
    delay(100);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward); //turn left
    delay(100);
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(100);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn 180 degrees
    delay(100);
  }
}
long ping()
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear(); 
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println(); // this is for serial monitoring of the ping signal
  return inches;
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but i am out of pins
Use a serial LCD, instead. Free up a bunch a pins.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok so at some point in the future i want to add more sensors to my robot but i am out of pins how would i break this code down so i could use two arduinos via I2C with one running the sensors and the other running just the servos

That could very well be a nice way to divide and conquer, but given your current level of experience, I suspect it would be a struggle. Consider upgrading to a Mega instead.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i will think about that but it all depends on whether or not the sensors and stuff i want to add will work with it
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so far my robot is working well next step is to build a chassis and then eventually add more sensors possibly even that new pixy cam that i saw on kickstarter and a beaglebone or raspberry pi for wireless transmission of video and data
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1899
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would hold back on buying a pi.
I have one lying around doing nothing for over a year now. I wanted to use it to replace a network shield to go to a wireless router. However later I found out that you can connect the arduino directly to a hacked wireless router. I have connected my arduino mega to a openwrt supported wireless router via the serial port.  This is basically what the yun is about (but then with a leonardo).

The yun (to be released in september) is already integrated with arduino and aims at the wireless powerful devices.
This is nearly the same as my solution but Arduino has solved (most of?) the communication problems for you. For instance most of those routers have very little memory and even worse eeprom and the yun has had a redesign to add more memory and eeprom.
The advantage of a router is that you can have plenty of other network devices connected (like IP cams) and you can pick the arduino of your choise (like a mega :-) ).
If the yun USB capabilities are good; one could also add a USB hub and USB cameras. Though a USB camera will put more load on the linux than a pure IP cam. But as the yun has more memory and disk space as normal router that may not be a problem.
By the way I ordered 2 pixy cams :-D
Best regards
Jantje

PS Now I think about it. It may be possible to use the yun as a wireless router and add a ethernet hub :-D
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well if the yun has better processing than the beaglebone black ill think about it
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1899
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

well if the yun has better processing than the beaglebone black ill think about it
On processing level beaglebone beats yun easily. But do you need it?
And how well does beaglebone integrate with Arduino?
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

from what ive read both beaglebone and raspberry pi will integrate with an arduino
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

uh oh i think something broke i went to test it out again to show some buddies and for some reason my right servo does not stop when the danger thresh hold is met the rest of the parts do what they are supposed to

Code:
#include<Servo.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // lcd screen pins
const int pingPin = 4; //declares the ping pin
const int irPin = 0;  //Sharp infrared sensor pin
const int RForward = 0; //full speed forward
const int RBackward = 180; //full speed reverse
const int LForward = RBackward; //this decides the motor control
const int LBackward = RForward; //this decides the motor control
const int RNeutral = 90; //nuetral for motors
const int LNeutral = 90; //constants for motor speed
const int dangerThresh = 5; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  //this is the "neck" of the robot
Servo leftMotor; //declare left servo motor
Servo rightMotor; //declare right servo motor
long duration; //time it takes to recieve PING))) signal

void setup()
{
  Serial.begin(9600); //allows for serial monitoring
  lcd.begin(16, 2); //turn on the lcd screen
  lcd.print("Hello Jason");
  delay(250);
  lcd.clear();
  lcd.print("Activating KITT");
  delay(250);
  lcd.clear();
  rightMotor.attach(5); //attach left servo motor to proper pin
  leftMotor.attach(6); //attach right servo motor to proper pin
  panMotor.attach(3); //attach pan servo motor to proper pin
  panMotor.write(90); //center the "neck" of the robot
  delay(50);
  lcd.print("KITT Online");
  delay(250);
  lcd.clear();
 
}


void loop()
{
   
  int distanceFwd = ping(); //tells the robot distance forward
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward);
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); //stop forward motion
    panMotor.write(0);
    delay(100);
    rightDistance = ping(); //scan to the right
    delay(100);
    panMotor.write(180);
    delay(100);
    leftDistance = ping(); //scan to the left
    delay(100);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward); //turn left
    delay(100);
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(100);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn 180 degrees
    delay(100);
  }
}
long ping()
{
  long duration, inches;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  inches = microsecondsToInches(duration);
  lcd.print(inches);
  lcd.print("in to obstacle");
  delay(100);
  lcd.clear(); 
  delay(100);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.println(); // this is for serial monitoring of the ping signal
  return inches;
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh my i feel like an idiot went back through my wiring and found a loose ground it works now
Logged

Pages: 1 2 [3]   Go Up
Jump to: