Show Posts
Pages: [1] 2 3
1  Using Arduino / Project Guidance / Re: counter with 7 segment LED serial display on: February 07, 2013, 07:45:17 am
Great...I have the sample code that came with the board, but I'm still a little confused on how to hook it up to the uno board (IE: CS/LOAD, CLK, AND DIN) should go where on the board.
2  Using Arduino / Project Guidance / Re: counter with 7 segment LED serial display on: February 06, 2013, 06:42:39 pm
I am so new to this it is not even funny.  My student wanted to make a valentine counter so everytime a box was opened it would tell her that she got a new valentine (5 grade projects).  I searched on line and found this https://www.youtube.com/watch?v=wfHCKhXEM6Y  I repurposed the code as shown but for some reason the display will not go on.  It gives me a little flicker when it is connected, but that's it.


http://www.amazon.com/gp/product/B00A8H6BQ2/ref=oh_details_o02_s00_i00
3  Using Arduino / Project Guidance / counter with 7 segment LED serial display on: February 06, 2013, 06:16:41 pm
I trying to write a program to count every time a button is pushed and display on a 4 digit-7 segment LED display.  Through the serial monitor, the code registers the button push, but the LED display doesn't even light up.  Honestly, I don't know how to test it.  The display has 5 pins off the side.  5V for power and GND for ground are together and DIN, LOAD/CS, AND CLK grouped together.  I am only connecting the DIN to the TX on the arduino pin.  Do the other have to be connected as well and if so where?

Code:
#include <LiquidCrystal.h>

#include <SoftwareSerial.h>  //for software serial communication



#define txPin 1  //change to your serial port on Arduino board
#define rxPin 0  //not used but is required

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
int buttonPressCount;


const int  buttonPin = 9;    //the pin that the pushbutton is attached to




int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button


void setup()  {
  pinMode(buttonPin, INPUT);  //initialize the button pin as a input
  Serial.begin(9600);  //initialize serial communication

  pinMode(txPin, OUTPUT);
  //the following resets the board, changes the brightness to 100%, and sets the board to '0000':
  mySerial.begin(9600);
  mySerial.write((byte)0x7A); //special character
  mySerial.write((byte)0x00); //set brightness to full
  mySerial.write((byte)0x76); //reset board
  mySerial.print(0); //send '0' character
  mySerial.print(0); //send '0' character
  mySerial.print(0); //send '0' character
  mySerial.print(0); //send '0' character
}

void loop(){

  buttonState = digitalRead(buttonPin);  //read the pushbutton input pin

    // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;

      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      updateDisplay(buttonPushCounter);  //function to update the display 'requires button press count'


    }

  }

  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop



}




void updateDisplay(int buttonPushCounter){
  String intString = String(buttonPushCounter);  //changes integer to a string
  char displayChars[4];  //create array to hold the four numbers
  int stringLength = intString.length();  //get length of the string
  //the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
  //and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
  if(stringLength == 4){
    displayChars[0] = intString.charAt(0);
    displayChars[1] = intString.charAt(1);
    displayChars[2] = intString.charAt(2);
    displayChars[3] = intString.charAt(3);
  }
  else if(stringLength == 3){
    displayChars[0] = 0;
    displayChars[1] = intString.charAt(0);
    displayChars[2] = intString.charAt(1);
    displayChars[3] = intString.charAt(2);
  }
  else if(stringLength == 2){
    displayChars[0] = 0;
    displayChars[1] = 0;
    displayChars[2] = intString.charAt(0);
    displayChars[3] = intString.charAt(1);
  }
  else if(stringLength == 1){
    displayChars[0] = 0;
    displayChars[1] = 0;
    displayChars[2] = 0;
    displayChars[3] = intString.charAt(0);
  }
  mySerial.write((byte)0x76); //Reset board
  mySerial.write((byte)0x76); //Reset board
  mySerial.print(displayChars[0]); //Send '0' character
  mySerial.print(displayChars[1]); //Send '0' character
  mySerial.print(displayChars[2]); //Send '0' character
  mySerial.print(displayChars[3]); //Send '0' character

  delay(100); //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'

}



4  Using Arduino / Displays / counter with 7 segment LED serial display on: February 06, 2013, 05:39:29 pm
I trying to write a program to count every time a button is pushed.  Through the serial monitor, code registers the button push, but the LED display doesn't even light up.  Honestly, I don't know how to test it.  The display has 5 pins off the side.  5V for power and GND for ground together and DIN, LOAD/CS, AND CLK grouped together.  I am only connecting the DIN pin.  Do the other have to be connected as well and if so where?

Code:
#include <SoftwareSerial.h>  //for software serial communication



#define txPin 1  //change to your serial port on Arduino board
#define rxPin 0  //not used but is required

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
int buttonPressCount;


const int  buttonPin = 9;    //the pin that the pushbutton is attached to




int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button


void setup()  {
  pinMode(buttonPin, INPUT);  //initialize the button pin as a input
  Serial.begin(9600);  //initialize serial communication

  pinMode(txPin, OUTPUT);
  //the following resets the board, changes the brightness to 100%, and sets the board to '0000':
  mySerial.begin(9600);
  mySerial.write((byte)0x7A); //special character
  mySerial.write((byte)0x00); //set brightness to full
  mySerial.write((byte)0x76); //reset board
  mySerial.print(0); //send '0' character
  mySerial.print(0); //send '0' character
  mySerial.print(0); //send '0' character
  mySerial.print(0); //send '0' character
}

void loop(){

  buttonState = digitalRead(buttonPin);  //read the pushbutton input pin

    // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;

      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      updateDisplay(buttonPushCounter);  //function to update the display 'requires button press count'


    }

  }

  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop



}




void updateDisplay(int buttonPushCounter){
  String intString = String(buttonPushCounter);  //changes integer to a string
  char displayChars[4];  //create array to hold the four numbers
  int stringLength = intString.length();  //get length of the string
  //the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
  //and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
  if(stringLength == 4){
    displayChars[0] = intString.charAt(0);
    displayChars[1] = intString.charAt(1);
    displayChars[2] = intString.charAt(2);
    displayChars[3] = intString.charAt(3);
  }
  else if(stringLength == 3){
    displayChars[0] = 0;
    displayChars[1] = intString.charAt(0);
    displayChars[2] = intString.charAt(1);
    displayChars[3] = intString.charAt(2);
  }
  else if(stringLength == 2){
    displayChars[0] = 0;
    displayChars[1] = 0;
    displayChars[2] = intString.charAt(0);
    displayChars[3] = intString.charAt(1);
  }
  else if(stringLength == 1){
    displayChars[0] = 0;
    displayChars[1] = 0;
    displayChars[2] = 0;
    displayChars[3] = intString.charAt(0);
  }
  mySerial.write((byte)0x76); //Reset board
  mySerial.write((byte)0x76); //Reset board
  mySerial.print(displayChars[0]); //Send '0' character
  mySerial.print(displayChars[1]); //Send '0' character
  mySerial.print(displayChars[2]); //Send '0' character
  mySerial.print(displayChars[3]); //Send '0' character

  delay(100); //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'

}



5  Using Arduino / General Electronics / 4 digit LED serial display on: February 05, 2013, 07:41:27 pm
I have a 4 digit LED serial display which came in two-two digit displays soldered together on a board.  There are 5 pins off the side:

5v = power
GND = ground

DIN = ?
LOAD/CS = ?
CLK = ?

What do the those mean?

Ultimately I need to build a counter with a student.  Basically when a button is pushed it will count on the display.  Any help someone can provide would be great.

thanks...frustrated teacher
6  Using Arduino / Programming Questions / Re: arduino counter on: February 05, 2013, 07:30:06 pm
Thanks....it compiled great.  but unfortunately, I can't get the LED 4 digit counter to light. 
7  Using Arduino / Programming Questions / arduino counter on: February 05, 2013, 06:37:06 pm
I'm trying to build a counter...basically everytime you push a button, the counter will register another.  I have a serial 7 segment LED display...the code I'm repurposing is below which was written for a arduino mega whereas I have an uno.  I have two questions:  First.  the display has three pins:  DIN, LOAD/CS, AND CLK..which led will actually be used for the display.  Second, I get a code error of "As of Arduino 1.0, the 'BYTE' keyword is no longer supported.  Please use Serial.write() instead"  Exactly how do I write that change.


thanks


Code:
#include <SoftwareSerial.h>  //for software serial communication



#define txPin 10  //change to your serial port on Arduino board
#define rxPin 15  //not used but is required

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
int buttonPressCount;


const int  buttonPin = 2;    //the pin that the pushbutton is attached to




int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button


void setup()  {
  pinMode(buttonPin, INPUT);  //initialize the button pin as a input
  Serial.begin(9600);  //initialize serial communication

 pinMode(txPin, OUTPUT);
 //the following resets the board, changes the brightness to 100%, and sets the board to '0000':
 mySerial.begin(9600);
 mySerial.print(0x7A,BYTE); //special character
 mySerial.print(0x00,BYTE); //set brightness to full
 mySerial.print(0x76,BYTE); //reset board
 mySerial.print(0); //send '0' character
 mySerial.print(0); //send '0' character
 mySerial.print(0); //send '0' character
 mySerial.print(0); //send '0' character
}

void loop(){
 
  buttonState = digitalRead(buttonPin);  //read the pushbutton input pin

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
     
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      updateDisplay(buttonPushCounter);  //function to update the display 'requires button press count'
     
     
    }

  }
 
  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop

 

}




void updateDisplay(int buttonPushCounter){
String intString = String(buttonPushCounter);  //changes integer to a string
char displayChars[4];  //create array to hold the four numbers
int stringLength = intString.length();  //get length of the string
//the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
//and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
if(stringLength == 4){
  displayChars[0] = intString.charAt(0);
  displayChars[1] = intString.charAt(1);
  displayChars[2] = intString.charAt(2);
  displayChars[3] = intString.charAt(3);
}else if(stringLength == 3){
  displayChars[0] = 0;
  displayChars[1] = intString.charAt(0);
  displayChars[2] = intString.charAt(1);
  displayChars[3] = intString.charAt(2);
}else if(stringLength == 2){
  displayChars[0] = 0;
  displayChars[1] = 0;
  displayChars[2] = intString.charAt(0);
  displayChars[3] = intString.charAt(1);
}else if(stringLength == 1){
  displayChars[0] = 0;
  displayChars[1] = 0;
  displayChars[2] = 0;
  displayChars[3] = intString.charAt(0);
}
 mySerial.print(0x76,BYTE); //Reset board
 mySerial.print(0x76,BYTE); //Reset board
 mySerial.print(displayChars[0]); //Send '0' character
 mySerial.print(displayChars[1]); //Send '0' character
 mySerial.print(displayChars[2]); //Send '0' character
 mySerial.print(displayChars[3]); //Send '0' character

delay(100); //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'
 
}


8  Using Arduino / Project Guidance / Re: code suggestions on: January 09, 2013, 11:28:47 am
Thanks for the info...yes, could you please post the code.
9  Using Arduino / Project Guidance / Re: code suggestions on: January 09, 2013, 08:54:14 am
Now that you mention it, the serial prints are a bit erratic.  Sometimes they drop to zero randomly, but I just assumed the ping sensor was resetting.  I am currently using female jumper wire to run from the sensor to a standard jumper wire to go into the board.  Should I try to simplify?
Thanks
10  Using Arduino / Project Guidance / Re: code suggestions on: January 08, 2013, 06:26:04 pm
The ping sensor is securely mounted however all the wiring is done with jumper wires.  Could that still be the issue?

11  Using Arduino / Project Guidance / Re: code suggestions on: January 08, 2013, 06:14:07 pm
Actually no.  Because I didn't want to solder the sensor I'm using jumper wires to connect the sensor to a breadboard.  Would that really make the difference?
12  Using Arduino / Project Guidance / code suggestions on: January 08, 2013, 05:47:28 pm
I've been working on a wall sensing robot that will turn when it comes close to a wall and I'm on a newer version of my code.  It's buggy and I can't figure out the issue.  When I turn the robot on, it goes for a second or two, turns, goes a bit more, turns again...this just keeps repeating.  In don't know if it is actually picking up the sensor readings or not.

Here is a you tube video of what it is currently doing



Code:
// names the LED and pin number
int led = 10;
int led2 = 6;


// this constant won't change.  It's the pin number for the sensor
// of the sensor's output:
const int pingPin = 7;

//sets the interger for the pulse sensor
long distance;
unsigned long pulseDuration=0;

void setup() {

  //Setup MOTOR RIGHT
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  //Setup MOTOR LEFT
  pinMode(13, OUTPUT); //Initiates Motor Channel A pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel A pin

  //SETUP LED HEADLIGHTS
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode (led2, OUTPUT);

  // initialize serial communication: SENSOR
  Serial.begin(9600);
}

void loop (){
  distance = pingsensor();
  //for debugging
  Serial.println(distance);
  if (distance < 5){
    turnAround();
  }
  else {
    goForward();   
  }
}


long pingsensor(){
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  return inches;
  //delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

void goForward(){
  //Motor A forward @ full speed (left motor)
  //full speed of motor is 255
  digitalWrite(12, LOW);  //Establishes forward direction of Channel A  left motor
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 150);    //Spins the motor on Channel A

  //motor B forward @ full speed (right motor)
  digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor
  digitalWrite (8, LOW); //Disengages the brake for Channel B
  analogWrite (11, 150);  //Spins the motor on Channel B
}

void turnAround(){
  //engages the brake for left motor
  digitalWrite(9, HIGH); 

  //DELAY FOR LONG ENOUGH TO TURN, CHANGE AS NEEDED
  delay(2000);

}
13  Using Arduino / Project Guidance / Re: code question on: January 07, 2013, 07:49:04 pm
really what I want is for the robot to turn as it goes back.  As I go through all this I'm trying understand the code as I go through it (walking before running, if you will).  Here is my current code but I get the following error

new_robot_sketch.ino: In function 'void motion()':
new_robot_sketch:116: error: expected `;' before 'digitalWrite'


Code:
// names the LED and pin number
int led = 10;
int led2 = 6;


// this constant won't change.  It's the pin number for the sensor
// of the sensor's output:
const int pingPin = 7;

//sets the interger for the pulse sensor
long distance;
unsigned long pulseDuration=0;

void setup() {

 //Setup MOTOR RIGHT
 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
 pinMode(9, OUTPUT); //Initiates Brake Channel A pin

 //Setup MOTOR LEFT
 pinMode(13, OUTPUT); //Initiates Motor Channel A pin
 pinMode(8, OUTPUT);  //Initiates Brake Channel A pin

 //SETUP LED HEADLIGHTS
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);
  pinMode (led2, OUTPUT);

  // initialize serial communication: SENSOR
 Serial.begin(9600);
}
 
 void loop (){
   distance = pingsensor();
   motion ();
 }
 
   
 long pingsensor(){
 // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
 long duration, inches, cm;

 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 // The same pin is used to read the signal from the PING))): a HIGH
 // pulse whose duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);

 // convert the time into a distance
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);

 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();
 
  return inches;
 //delay(100);
}

long microsecondsToInches(long microseconds)
{
 // According to Parallax's datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}

void motion (){

// distance IN INCHES at which the robot will stop
 if (distance > 5){
 
 //Motor A forward @ full speed (left motor)
 //full speed of motor is 255
   digitalWrite(12, LOW);  //Establishes forward direction of Channel A  left motor
   digitalWrite(9, LOW);   //Disengage the Brake for Channel A
   analogWrite(3, 239);    //Spins the motor on Channel A

 //motor B forward @ full speed (right motor)
   digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor
   digitalWrite (8, LOW); //Disengages the brake for Channel B
   analogWrite (11, 254);  //Spins the motor on Channel B
 }
 
 else {
 // stop motors
//engages the brake for left motor
   digitalWrite(9, HIGH);
//engages brake for right motorch
   digitalWrite (8, HIGH);
   
   delay (5000)
   
   digitalWrite (12, HIGH);  //establishes a backwards motion
   digitalWrite (9, LOW);  //disengages brake for channel A
   analogWrite (3, 239);  //establishes the speed for channel A
   
   digitalWrite (13, LOW);  //establishes a backwards motion
   digitalWrite (8, LOW);  //disengages brake for channel B
   analogWrite (11, 100);  //establishes speed of channel B
   
 }
}
}



 
   
14  Using Arduino / Project Guidance / Re: code question on: January 07, 2013, 07:01:46 pm
I'm sorry, I don't think I made myself clear.  I know how to get the wheels backwards, just set the pins to the opposite value.  Ultimately what I want it do is after the robot stops, I want it to move backwards.  I'm not sure where that code goes.  Is it part of the "else" statement or is it an entirely different function.
15  Using Arduino / Project Guidance / Re: code question on: January 07, 2013, 06:45:49 pm
If I want the robot to backup (in a straight line) after it stops, where would the code go for that?  Attached is the working code that stops the robot when it comes close to a wall.

Code:
// names the LED and pin number
int led = 10;
int led2 = 6;


// this constant won't change.  It's the pin number for the sensor
// of the sensor's output:
const int pingPin = 7;

//sets the interger for the pulse sensor
long distance;
unsigned long pulseDuration=0;

void setup() {

 //Setup MOTOR RIGHT
 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
 pinMode(9, OUTPUT); //Initiates Brake Channel A pin

 //Setup MOTOR LEFT
 pinMode(13, OUTPUT); //Initiates Motor Channel A pin
 pinMode(8, OUTPUT);  //Initiates Brake Channel A pin

 //SETUP LED HEADLIGHTS
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);
  pinMode (led2, OUTPUT);

  // initialize serial communication: SENSOR
 Serial.begin(9600);
}
 
 void loop (){
   distance = pingsensor();
   motion ();
   turn ();
 }
 
   
 long pingsensor(){
 // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
 long duration, inches, cm;

 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 // The same pin is used to read the signal from the PING))): a HIGH
 // pulse whose duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);

 // convert the time into a distance
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);

 Serial.print(inches);
 Serial.print("in, ");
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();
 
  return inches;
 delay(100);
}

long microsecondsToInches(long microseconds)
{
 // According to Parallax's datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}

void motion (){

// distance IN INCHES at which the robot will stop
 if (distance > 5){
 
 //Motor A forward @ full speed (left motor)
 //full speed of motor is 255
   digitalWrite(12, LOW);  //Establishes forward direction of Channel A  left motor
   digitalWrite(9, LOW);   //Disengage the Brake for Channel A
   analogWrite(3, 239);    //Spins the motor on Channel A

 //motor B forward @ full speed (right motor)
   digitalWrite (13, HIGH); //Establishes forward motion of Channel B right motor
   digitalWrite (8, LOW); //Disengages the brake for Channel B
   analogWrite (11, 254);  //Spins the motor on Channel B
 }
 
 else {
 // stop motors
//engages the brake for left motor
   digitalWrite(9, HIGH);
   //engages brake for right motorch
   digitalWrite (8, HIGH);
 }
}
Pages: [1] 2 3