Go Down

Topic: Please Help!! (Read 4585 times) previous topic - next topic

blitz22

I managed to FINALLY get it to do what i wanted! i have been using the buzzer but it still gives me a delay when goes off even though the if statement is still true.thank you very much for all the help and i feel that my lack of understanding and knowledge probably pissed you off so im sorry for that!

here is the final code, if you feel like you've not reached the end of your tether just yet then i'm sure you'll correct me  :) but if not the THANKS A LOT!
Code: [Select]
byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;
float distance;
int buzzer = 10;
byte red=7;
byte yellow=8;
byte green=4;
float distanceread;
void setup (){
  pinMode(buzzer, OUTPUT);
  pinMode (triggerright, OUTPUT);
  pinMode (echoright, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  Serial.begin (9600);    // set up the baud rate for serial printing
}

void loop (){

  distanceread = getDistance ();   

  if(getDistance ()<=.25 && getDistance () >=0)
  {       
    digitalWrite(red, HIGH);
     
    analogWrite (buzzer,128);
    delay (100);
    analogWrite (buzzer,0);
   
  }
else {
  digitalWrite(red, LOW);
  analogWrite (buzzer,0);
  }
         

  if(getDistance ()<=.5 && getDistance () >=.25)
  {       
    digitalWrite(yellow, HIGH);
    getDistance();
  }
else { 
digitalWrite(yellow, LOW);
}
       

 
if(getDistance ()<=1 && getDistance () >=0.5)
{       
    digitalWrite(green, HIGH);
    getDistance();
  }
else {
  digitalWrite(green, LOW);
  }
     
  Serial.print(getDistance());     
  Serial.print("m");         
  Serial.println();           
  delay (2000);               




}

float getDistance ()
{
  digitalWrite(triggerright, LOW);   
  delayMicroseconds(2);
  digitalWrite(triggerright, HIGH);   
  delayMicroseconds(5);
  digitalWrite(triggerright, LOW);   


  float distance = pulseIn (echoright, HIGH); 

  return (distance) / 2938 / 2;
}







tnovak

#46
Apr 30, 2011, 03:38 am Last Edit: Apr 30, 2011, 04:32 am by tnovak Reason: 1
Just to let people know, when i introduced the "while loop" idea, at the beginning of this thread, it was supposed to stay in that loop until the while statement was false by calling the getDistance() function.  DONT USE THIS AS YOU ARE FIGURING IT OUT ON YOUR OWN... kinda....

Code: [Select]


while (distance =< .25){
buzz(100);
digitalWrite(redled, HIGH);
distance = getDistance();
digitalWrite(redled, LOW);
}

while (distance <= .5 && distance > .25){
buzz(400);
digitalWrite(yellowled, HIGH);
distance = getDistance();
digitalWrite(yellowled, LOW);
}

while (distance <= 1 && distance > .5){
buzz(700);
digitalWrite(greenled, HIGH);
distance = getDistance();
digitalWrite(greenled, LOW);
}


It was meant to stay in the while loop while it was true, as it changed it would go to the next true loop and stay there until it changed. Using no delays. Bit banging the LED, and using buzz function for sound.(made quickly from blinkWithoutDelay).

But back to your most recent code:

Code: [Select]

distanceread = getDistance ();  

 if(getDistance ()<=.25 && getDistance () >=0)
 {      
   digitalWrite(red, HIGH);
   
   analogWrite (buzzer,128);
   delay (100);
   analogWrite (buzzer,0);
   
 }
else {
 digitalWrite(red, LOW);
 analogWrite (buzzer,0);
 }


You set the variable distanceread but never used it. You read the distance twice in one "if comparison".

Again blinkWithoutDelay could really help you with the buzzer and also serial printing every to seconds without stopping your code.

You also deleted your call to getDistance() in the if statement, when you added the buzzer.

tnovak

Something using a short version of blink without delay to print every 2 seconds would be...

Code: [Select]

currentMillis = millis();
if(currentMillis - previousMillis > 2000) {
  previousMillis = currentMillis;   
  Serial.print(distanceread);     
  Serial.print("m");         
  Serial.println();
}


So as long as you don't use any delays in your code this will print your "distanceread" value approximately every 2 seconds. If you can grasp that concept you can use it for the buzzer as well. Your code is short enough and fast enough (without using delay) to make this work. (I did test on a arduino uno)

Here is an cleaned up example, of using a version of blinkWithoutDelay for buzzing on/off at a certain interval. It uses buzz(1000); for a 1 second on and off sound. There is some stuff that would have to be addressed if you used for long periods of time.

Code: [Select]

void buzz(int interval)
{
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
  if (buzzerState == 0){
    buzzerState = 128;
  } else {
    buzzerState = 0;}
  analogWrite(buzzer, buzzerState);
}}

bubulindo


bubulindo,
i am studying a mechanical engineering degree and i have so much more work than this plus exams to revise for and i am not looking for a career in electronics.


You'd be amazed at how many Mechanical Engineers need programming skills...
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

blitz22

using the blinkwithoutdelay code as an example is it possible to state how long the LED is on and off for? the tutorial blinks the LED roughly for a second and then off for a second. (tnovak i know I've asked you already)

AWOL

Quote
using the blinkwithoutdelay code as an example is it possible to state how long the LED is on and off for?

Yes.
The clue is in the call to "millis ()"  (milliseconds) and the constant "1000"
"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.

blitz22

ive played around with the values but no change really.the time seems to still be constant for on and off

AWOL

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

blitz22

i changed the interval to 500 and it now seems to be on for longer...
Code: [Select]
byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;
float distance;
const int buzzer = 10;
byte red=7;
byte yellow=8;
byte green=4;
float distanceread;
long previousMillis=0;
int buzzerState = LOW;
long interval = 500;
void setup (){
  pinMode(buzzer, OUTPUT);
  pinMode (triggerright, OUTPUT);
  pinMode (echoright, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  Serial.begin (9600);    // set up the baud rate for serial printing
}

void loop (){

  distanceread = getDistance ();



  if(getDistance ()<=.25 && getDistance () >=0)
  {       
    digitalWrite(red, HIGH);
    getDistance();
    analogWrite (buzzer,128);
    delay (100);
    analogWrite (buzzer,0);

  }
  else {
    digitalWrite(red, LOW);
    analogWrite (buzzer,0);
  }


  if(getDistance ()<=.5 && getDistance () >=.25)
  {       
    digitalWrite(yellow, HIGH);
    getDistance();
  }
  else { 
    digitalWrite(yellow, LOW);
  }



  if(getDistance ()<=1 && getDistance () >=0.5)
  {       
    digitalWrite(green, HIGH);

    buzz (100);
    getDistance ();
  }
  else {
    digitalWrite(green, LOW);
  }


  Serial.print(getDistance());     
  Serial.print("m");         
  Serial.println();           
  delay (2000);               

}

float getDistance ()
{
  digitalWrite(triggerright, LOW);   
  delayMicroseconds(2);
  digitalWrite(triggerright, HIGH);   
  delayMicroseconds(5);
  digitalWrite(triggerright, LOW);   


  float distance = pulseIn (echoright, HIGH); 

  return (distance) / 2938 / 2;
}


void buzz(int interval)
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
    if (buzzerState == 0){
      buzzerState = 110;
    }
    else {
      buzzerState = 0;
    }
    analogWrite(buzzer, buzzerState);
   
  }
}






but how can i get it to be more frequent?

AWOL

#54
May 02, 2011, 08:34 pm Last Edit: May 02, 2011, 08:45 pm by AWOL Reason: 1
Blink without delay with an on time of 100 milliseconds and an off time of 900 milliseconds.
Code: [Select]
const int ledPin =  13;
const long onTime = 100;
const long offTime = 900;

int ledState = LOW;
long previousMillis = 0;
long interval;

void setup() {
 pinMode(ledPin, OUTPUT);      
}

void loop()
{
 unsigned long currentMillis = millis();

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

   // if the LED is off turn it on and vice-versa:
   if (ledState == LOW) {
     ledState = HIGH;
     interval = onTime;
   } else {
     ledState = LOW;
     interval = offTime;
   }  

   // set the LED with the ledState of the variable:
   digitalWrite(ledPin, ledState);
 }
}


Why are you asking questions about blink without delay when you've got delays in your code?

You've also still got calls to "getDistance(); " that don't do anything with the returned vlaue.
These just waste time.
You've even got a call to getDistance that saves the value returned, but you don't do anything with that value!

Code: [Select]
if(getDistance ()<=.25 && getDistance () >=0)

Does it seem likely getDistance is ever going to return a negative value?
"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.

blitz22

Quote
Why are you asking questions about blink without delay when you've got delays in your code?


i didn't think i did tbh
Quote
You've also still got calls to "getDistance(); " that don't do anything with the returned vlaue.


i want the program to re read the value to check if the if statement is still true

AWOL

#56
May 02, 2011, 08:50 pm Last Edit: May 02, 2011, 08:56 pm by AWOL Reason: 1
Code: [Select]
if(getDistance ()<=.5 && getDistance () >=.25)   // YOU'RE SLOWING THINGS DOWN - YOU READ distanceread AT THE TOP OF THE LOOP - USE THAT.
 {      
   digitalWrite(yellow, HIGH);
   getDistance(); // <<< YOU HAVEN'T DONE ANYTHING WITH THE VALUE THIS RETURNED == WASTED TIME
 }
 else {  
   digitalWrite(yellow, LOW);
 }


Quote
Why are you asking questions about blink without delay when you've got delays in your code?

i didn't think i did tbh


You can' search for "delay" if you have any doubts.

Quote
i want the program to re read the value to check if the if statement is still true


I think you're confusing an "if" with a "while"
"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.

blitz22

okay i have used your on and off time to dictate buzzer but now it says too few arguments in void buzz (int)...??
Code: [Select]
void buzz(int interval)
{
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
   
    if (buzzerState == 0){
      buzzerState = 110;
      interval = onTime;
    }
    else {
      buzzerState = 0;
      interval = offTime;
    }
    analogWrite(buzzer, buzzerState);
   
  }
}



AWOL

#58
May 02, 2011, 09:01 pm Last Edit: May 02, 2011, 09:05 pm by AWOL Reason: 1
I suppose it would do that.
But I can't see your code.

I'm also wondering why you're passing (or not) "interval" into "buzz"
"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.

blitz22

sorted out that problem but now it just stays on and never goess off (think ive gone deaf!)
Code: [Select]
byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;
float distance;
const int buzzer = 10;
byte red=7;
byte yellow=8;
byte green=4;
float distanceread;
const long onTime = 2000;
const long offTime = 1000;

int buzzerState = LOW;
long previousMillis = 0;
long interval;


void setup (){
  pinMode(buzzer, OUTPUT);
  pinMode (triggerright, OUTPUT);
  pinMode (echoright, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  Serial.begin (9600);    // set up the baud rate for serial printing
}

void loop (){

  distanceread = getDistance ();



  if(getDistance ()<=.25 && getDistance () >=0)
  {       
    digitalWrite(red, HIGH);
   buzz(100);
  }
  else {
    digitalWrite(red, LOW);
   
  }


  if(getDistance ()<=.5 && getDistance () >=.25)
  {       
    digitalWrite(yellow, HIGH);
     
  }
  else { 
    digitalWrite(yellow, LOW);
  }



  if(getDistance ()<=1 && getDistance () >=0.5)
  {       
    digitalWrite(green, HIGH);
   
  }
  else {
    digitalWrite(green, LOW);
   
  }


  Serial.print(getDistance());     
  Serial.print("m");         
  Serial.println();           
  delay (2000);               

}

float getDistance ()
{
  digitalWrite(triggerright, LOW);   
  delayMicroseconds(2);
  digitalWrite(triggerright, HIGH);   
  delayMicroseconds(5);
  digitalWrite(triggerright, LOW);   


  float distance = pulseIn (echoright, HIGH); 

  return (distance) / 2938 / 2;
}


void buzz(int interval)
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
   
    if (buzzerState == 0){
      buzzerState = 110;
      interval = onTime;
    }
    else {
      buzzerState = 0;
      interval = offTime;
    }
    analogWrite(buzzer, buzzerState);
   
  }
}






Go Up