Show Posts
|
|
Pages: [1] 2 3
|
|
6
|
Topics / Robotics / HS 311 only turns to the left
|
on: March 27, 2013, 05:49:12 pm
|
so i bought a HS 311 to use for the swivel mount for my ping sensor but for some odd reason the servo only turns left. I have been googling this problem since last night and really havent found an easy solution besides people saying buy another servo. /*MAEP 2.0 Navigation by Noah Moroze, aka GeneralGeek This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/ PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping Edited by: Jason Miller for Project KITT */
#include <Servo.h> //include Servo library #include <LiquidCrystal.h> //include lcd library
LiquidCrystal lcd(2, 3, 6, 7, 8, 9);
const int RForward = 0; const int RBackward = 180; const int LForward = RBackward; const int LBackward = RForward; const int RNeutral = 90; const int LNeutral = 90; //constants for motor speed const int pingPin = 4; const int dangerThresh = 5; //threshold for obstacles (in inches) int leftDistance, rightDistance; //distances on either side Servo panMotor; Servo leftMotor; Servo rightMotor; //declare motors long duration; //time it takes to recieve PING))) signal
void setup() { Serial.begin(9600); lcd.begin(16, 2); lcd.print("Hello Jason"); delay(1000); lcd.clear(); lcd.print("Activating KITT"); delay(1000); lcd.clear(); lcd.print("KITT Online"); delay(1000); rightMotor.attach(11); leftMotor.attach(10); panMotor.attach(5); //attach motors to proper pins panMotor.write(90); //set PING))) pan to center }
void loop() { int distanceFwd = ping(); if (distanceFwd>dangerThresh) //if path is clear { leftMotor.write(LForward); rightMotor.write(RForward); //move forward lcd.print("Forward"); } else //if path is blocked { leftMotor.write(LNeutral); rightMotor.write(RNeutral); panMotor.write(0); delay(500); rightDistance = ping(); //scan to the right lcd.print("Scan Right"); delay(500); panMotor.write(180); delay(700); leftDistance = ping(); //scan to the left lcd.print("Scan Left"); delay(500); 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 lcd.print("Turn Left"); delay(500); } else if (rightDistance>leftDistance) //if right is less obstructed { leftMotor.write(LForward); rightMotor.write(RBackward); //turn right lcd.print("Turn Right"); delay(500); } else //if they are equally obstructed { leftMotor.write(LForward); rightMotor.write(RBackward); //turn 180 degrees lcd.print("Turn 180"); delay(500); } }
long ping() { // Send out PING))) signal pulse long duration, inches; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); //Get duration it takes to receive echo pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); inches = microsecondsToInches(duration); lcd.print(inches); lcd.print("in to obstacle"); delay(100); lcd.clear(); delay(100); } long microsecondsToInches(long microseconds) { return duration / 29 / 2; }
|
|
|
|
|
7
|
Using Arduino / Programming Questions / Re: weird error in code pls help
|
on: March 23, 2013, 02:32:30 am
|
ok so i took out the data logging and just stuck with the ping and lcd now i just need a little help with getting the statements set up so my robot can avoid obstacles once ii can get my servos installed when they arrive #include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 6, 7, 8, 9); const int pingPin = 4;
void setup() { Serial.begin(9600); lcd.begin(16, 2); lcd.print("Hello Jason"); delay(1000); lcd.clear(); lcd.print("Activating KITT"); delay(1000); }
void loop() { 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); } long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; }
|
|
|
|
|
9
|
Using Arduino / Programming Questions / Re: weird error in code pls help
|
on: March 22, 2013, 12:17:06 pm
|
|
i tripled checked all the wiring and had it working from setup and now all i have is a blinking cursor. since this will not be connected to a computer it needs to have the lcd screen displaying exactly the same information that its data logging
|
|
|
|
|
10
|
Using Arduino / Programming Questions / Re: weird error in code pls help
|
on: March 22, 2013, 01:54:27 am
|
ok i kinda got it working but now all my lcd screen shows is a blinking cursor and no messages #include <SPI.h> #include <SD.h> #include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 6, 7, 8, 9); const int chipSelect = 4; const int pingPin = 2; File dataFile;
void setup() { lcd.begin(16, 2); Serial.begin(9600); lcd.print("Initializing SD card..."); pinMode(10, OUTPUT); delay(250); lcd.clear(); delay(250); if (!SD.begin(chipSelect)) { lcd.println("Card failed."); // don't do anything more: return; } lcd.println("card initialized."); }
void loop() { lcd.clear(); long duration, inches, cm; pinMode(pingPin, OUTPUT); String dataString = ""; int sensor =0;
digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);
sensor = digitalRead(pingPin); dataString += String(sensor); dataFile = SD.open("datalog.txt", FILE_WRITE);
inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { dataFile.println(dataString); dataFile.close(); Serial.println(dataString); }//if else { Serial.println("error opening datalog.txt"); }//else
Serial.print(inches); lcd.print("in, "); Serial.print(cm); lcd.print("cm");
delay(100); } long microsecondsToInches(long microseconds) { return (microseconds / 74 ) / 2; }//microsecondsToInches
long microsecondsToCentimeters(long microseconds) { return (microseconds / 29) / 2; }//microsecondsToCentimeters
|
|
|
|
|
12
|
Using Arduino / Programming Questions / Re: weird error in code pls help
|
on: March 22, 2013, 01:08:53 am
|
i wound up scrapping that code nd starting over but im still getting errors so here is the new code #include <SPI.h> #include <SD.h> #include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 6, 7, 8, 9); const int chipSelect = 4; const int pingPin = 2;
void setup() { lcd.begin(16, 2); Serial.begin(9600); lcd.print("Initializing SD card..."); pinMode(10, OUTPUT); delay(250); lcd.clear(); delay(250); if (!SD.begin(chipSelect)) { lcd.println("Card failed."); // don't do anything more: return; } lcd.println("card initialized."); }
void loop() { String dataString = ""; int sensor = digitalRead(pingPin); dataString += String(sensor); } File dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } else { lcd.println("error opening datalog.txt"); } } } and here are the errors sketch_mar21b:32: error: expected unqualified-id before 'if' sketch_mar21b:38: error: expected unqualified-id before 'else' sketch_mar21b:41: error: expected declaration before '}' token and once again i do appologize for my poor coding skills i missed alot of programming labs due to a car accident last month
|
|
|
|
|
13
|
Using Arduino / Programming Questions / Re: weird error in code pls help
|
on: March 21, 2013, 09:11:49 pm
|
|
i have 2 force sensors to add to this and 2 continuous rotation servos once i get everything figured out.. the lcd screen is supposed to display the messages about the card slot as well as displaying distance from the ping sensor
|
|
|
|
|
14
|
Using Arduino / Programming Questions / Re: weird error in code pls help
|
on: March 21, 2013, 08:51:43 pm
|
ok here is my new code but my lcd screen is not working and i have tested the screen by itself and it does work but for some reason not with my code can any one show me what mistakes im making? #include <SPI.h> #include <SD.h>
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
#define OLED_DC 4 #define OLED_CS 6 #define OLED_CLK 5 #define OLED_MOSI 3 #define OLED_RESET 13 Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
const int chipSelect = 10; const int pingPin = 7;
void setup() { Serial.begin(9600); display.begin(SSD1306_SWITCHCAPVCC); display.display(); delay(2000); display.clearDisplay(); display.print("Initializing SD card..."); pinMode(10, OUTPUT); if (!SD.begin(chipSelect)) { display.println("Card failed, or not present"); return; } display.println("card initialized.");
}
void loop() { long duration, inches, cm; 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); cm = microsecondsToCentimeters(duration); Serial.print(inches); display.print("in, "); Serial.print(cm); display.print("cm"); Serial.println();
delay(100);
String dataString = "";
int sensor = digitalRead(pingPin); dataString += String(sensor); if (pingPin < 2) { dataString += ","; } File dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) { dataFile.println(dataString); dataFile.close(); Serial.println(dataString); } else { display.println("error opening datalog.txt"); } }
long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }
|
|
|
|
|