Go Down

Topic: Help to Ping only every 5 sec, and also how to send digits to my 7-seg.display!? (Read 951 times) previous topic - next topic

iching79

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 http://arduino.cc/forum/index.php?action=printpage;topic=106043.0, 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) :
Code: [Select]
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  :smiley-eek:
E

AWOL

Quote
To have my parallaxPing-sensor ping every 5 sec,

I can't see your code on my phone, but have you thought of using the technique in the blink without delay example?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

iching79

Yes,
but I didnt get it to work!

I tried this, but the result wasnt good, I abandoned it:

long previousMillis = 0;
long interval = 500;

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
 previousMillis = currentMillis;



E

PaulS

Quote
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...

Suppose the timeframe was 5 minutes, instead. How would YOU do it? You have a pad of paper and pencil, to doodle with, or maybe to write down last time you pinged, and a watch.

The variables in the code replace the pad of paper and pencil, except you can't doodle with them. The millis() function replaces the watch. The question to answer is is it time to ping again?

Quote
I recently looked into Teckels NewPing Library, which looks pretty smooth, but I cant manage to implement it into my code.

Why not?

Quote
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 stopped working. (and the dimming, depending on the photoresistor also use the BYTE keyword)

Why? The compiler practically told you what you needed to do.

Quote
cant I send numbers instead of the hex?

Numbers can be represented in binary, octal, hex, or decimal. If it makes it easier to understand, use any base that you like. I don't really recommend odd number bases, though. That's just weird.


AWOL

Code: [Select]
long interval = 500;A wait of 500 milliseconds is only half a second
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

iching79

AWOL: I know, I was just testing.. snipped the code from an old workfile.

PaulS: I have almost no skills with coding. I dont quite understand how I managed to make the code "semiwork" like it is now...
        The compiler said something, but I didn want to spend any more time on that issue because there maybe a much smarter way all together...

E

holmes4

Take a look at this bit of code. It will save you 600+ lines of code

Code: [Select]
int aNumber =27;
short firstDigit = 0;
short secondDigit =0;
void setup(){
Serial.begin(9600);
}

void loop(){
  Serial.print(" aNumber is ");
  Serial.println(aNumber);
 
  Serial.print("first digit is ");
  /* calc first digit as an int */
  firstDigit = aNumber/10;
  Serial.println(firstDigit);
 
  Serial.print("second digit is ");
  /* calc second digit as an int */
  secondDigit = aNumber - ((aNumber/10)*10);
  Serial.println(secondDigit);
  delay(10000);
 
}


Mark

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

iching79

Nick Gammon: I have a photoresistor connected, if there is low light, the display will dim, and when much light the display will brighten.
                    (The idea is that in much light I will need more brightness on the display to make it visible)

holmes4: This is exactly the kind of code im looking for! Though, I am not certain how to implement it with my 10-99 numbers..yet ;)

PaulS: I have changed "mySerial.print(0x76,BYTE);" with "mySerial.write(0x76);",
         but then I have to change the BYTE-command(or maybe omit it) with something else...
         If "BYTE" isnt allowed an longer, how do I make the display understand the first number from the second...?
         


     
E

AWOL

Quote
Nick Gammon: I have a photoresistor connected

The correct answer to Nick's question is "nothing", because you have a semicolon at the end of your "if"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

iching79

Hello again!

AWOL: I didnt notice that because it kinda worked! The display blinks very fast if the lightlevel i over 300 though...
         But it is dim in dim light and bright in bright light.

I have gone over the code and sorted the problem with the BYTE in 1.0.1.
I have also managed to get the NewPing.h to work. Well, I cant really say that it works, because the motor has some strange behaviour, still..

Is it possible to make the motor start for a certain amount of time after an if-statement, but without the delay? I want the display to run all the time so that the potensiometer updates fluently, but I want the motor to run ie. for 2 secs if this and that.....(while I update the display)?
In the "Blink without Delay" something is turned on or off at an interval,
I want to start the motor to run for some interval to...


:smiley-mr-green:

 

E

Arrch


Is it possible to make the motor start for a certain amount of time after an if-statement, but without the delay? I want the display to run all the time so that the potensiometer updates fluently, but I want the motor to run ie. for 2 secs if this and that.....(while I update the display)?
In the "Blink without Delay" something is turned on or off at an interval,
I want to start the motor to run for some interval to...


Yes, and all the tools you need are in the Blink Without Delay example, you just have to put together the logic to have it do that.

Go Up