Hello!
I have been fiddling around with this idea of mine for quite some time, but now im stuck!
I am pretty new to the Arduino and coding. I some times have issues with the brackets :~ So to any helpers:explain it like im 5.
Here is what i want to do:
To have my parallaxPing-sensor ping every 5 sec, and if the distance is over/under what my potensiometer is set to, start a motor forward or reverse for 1 sec.
Sounded simple, ehh...
I got my code working but it pings continually, making the motor sometimes go forward and reverse so fast that it makes the whole rig wiggle, making the ultrasonic readings totaly unusable.
I recently looked into Teckels NewPing Library Arduino Forum, which looks pretty smooth, but I cant manage to implement it into my code... (it has a median function which I also like)
At the release of 1.0.1 also the BYTE keyword is no longer supported, so my ridiculous list of if-tests giving digits to my display https://www.sparkfun.com/products/9767 stopped working. (and the dimming, depending on the photoresistor also use the BYTE keyword)
There has got to be a smarter way of sending numbers to the display!! My potensiometer is "mapped" to give numbers from 10 to 99. I dont quite get the ASCiihex-string either, cant I send numbers instead of the hex? I dont know how to implement "itoa", if thats what I need..
Here is my "thing". Etched the PCB, so I would like to not change any PINs etc if that is possible
My SuperDuperOptimized Code of Beauty 8) :
int potpin = 0;
const int enablePin = 3;
const int motorPin1 = 4;
const int motorPin2 = 5;
int pingsensor = 7;
int photocellpin = 2;
int potvalue;
const int topstopPin = 9;
#include <SoftwareSerial.h>
#define txPin 2
#define rxPin 12 //not used
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void setup(){
pinMode(enablePin, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(topstopPin, INPUT);
Serial.begin(9600);
pinMode(txPin, OUTPUT);
mySerial.begin(9600);
mySerial.print(0x76,BYTE); // Reset display command
mySerial.print(0x7A,BYTE); // Command byte
mySerial.print(0x1D,BYTE); // Dim display 0 til 254 i ASCii Hex - Lysere desto lavere nummer
mySerial.print(0x77,BYTE); // Command byte
mySerial.print(0x00,BYTE); // Display colon
}
int ping() //Get CM to obstacle in front of the sensor
{
long duration;
pinMode(pingsensor, OUTPUT);
digitalWrite(pingsensor, LOW);
delayMicroseconds(2);
digitalWrite(pingsensor, HIGH);
delayMicroseconds(15);
digitalWrite(pingsensor, LOW);
delayMicroseconds(20);
pinMode(pingsensor, INPUT);
duration = pulseIn(pingsensor, HIGH);
return duration / 29.0 / 2.0;
}
void loop(){
int val = digitalRead(topstopPin);
int photocellreading = analogRead(photocellpin);
if (photocellreading < 300);
mySerial.print(0x7A,BYTE); // Reset display command
mySerial.print(0xFE,BYTE); // Dimmest display command - Lysere desto lavere nummer
if (photocellreading > 300)
{
mySerial.print(0x7A,BYTE); // Reset display command
mySerial.print(0x09,BYTE); // Dim display 0 til 254 i ASCii Hex - Lysere desto lavere nummer (0x01) = Bright
} // 0x10 = litt mørkere (0xFE) = mørkest
potvalue = analogRead(potpin);
potvalue = map(potvalue, 0, 1023, 10, 99);
Serial.print(ping());
Serial.print(" cm Ping ");
Serial.print(potvalue);
Serial.print(" cm Potvalue");
Serial.println();
Serial.print(photocellreading);
Serial.print(" photocellreading");
Serial.println();
Serial.print(val);
Serial.print(" topstopPin");
Serial.println();
if (potvalue == 10)
{
mySerial.print(0x2D,BYTE); // left digit
mySerial.print(0x01,BYTE);
mySerial.print(0x00,BYTE);
mySerial.print(0x2D,BYTE);
}
if (potvalue == 11)
{
mySerial.print(0x2D,BYTE); // left digit
mySerial.print(0x01,BYTE);
mySerial.print(0x01,BYTE);
mySerial.print(0x2D,BYTE);
//This is repeated for all 10-99
if (potvalue == 95)
{
mySerial.print(0x2D,BYTE); // left digit
mySerial.print(0x09,BYTE);
mySerial.print(0x05,BYTE);
mySerial.print(0x2D,BYTE);
}
if (potvalue == 96)
{
mySerial.print(0x2D,BYTE); // left digit
mySerial.print(0x09,BYTE);
mySerial.print(0x06,BYTE);
mySerial.print(0x2D,BYTE);
}
if (potvalue == 97)
{
mySerial.print(0x2D,BYTE); // left digit
mySerial.print(0x09,BYTE);
mySerial.print(0x07,BYTE);
mySerial.print(0x2D,BYTE);
}
if (potvalue == 98)
{
mySerial.print(0x2D,BYTE); // left digit
mySerial.print(0x09,BYTE);
mySerial.print(0x08,BYTE);
mySerial.print(0x2D,BYTE);
}
if (potvalue == 99)
{
mySerial.print(0x2D,BYTE); // left digit
mySerial.print(0x09,BYTE);
mySerial.print(0x09,BYTE);
mySerial.print(0x2D,BYTE);
}
//if (val == HIGH)
//{
if (ping()+2 < potvalue && val == HIGH){
digitalWrite(motorPin1, HIGH); //Motor Opp
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 250);
delay(40);
}
else
{
analogWrite(enablePin, LOW); // motor stop
}
{
delay(15);
}
if (ping()-1 > potvalue)
{
digitalWrite(motorPin1, LOW); //Motor Ned
digitalWrite(motorPin2, HIGH);
analogWrite(enablePin, 250);
delay(40);
}
else
{
analogWrite(enablePin, LOW); // motor stop
}
//{
// delay(15);
//}
}
Hope any of you pro coders could find some time to help me,
Sincerly
Me
CodeForum.pde (16.4 KB)