Loading...
  Show Posts
Pages: 1 2 [3] 4 5
31  Using Arduino / Programming Questions / Re: sensor does not trigger during the delay time. on: April 30, 2011, 09:40:59 am
Your own version of blinkWithoutDelay is the best way to go. But until you learn the usage of it, try using an interrupt. Like....

Code:
         
attachInterrupt (1, breakout, RISING);       ///// Add this some where in your code, you will have to decide depending on your code

void breakout(){           //////////  Add this outside of the main loop
Out = 0;
}


When a button is pressed on pin 3, it set out to 0. So depending on where you put it(the attachInterupt), and if you redefine Out to soon(if you change the value of Out again before you use it), it should work. It looks like you have quite a bit of cleaning up to do just from what you posted. Cant really help without all the code.

http://arduino.cc/en/Reference/AttachInterrupt
32  Using Arduino / Programming Questions / Re: Way to run a program for a set number of seconds? on: April 30, 2011, 02:00:04 am
a vary simple way, would be to just add this line in your loop().

while(millis() > 210000){}

it will stop your code until you reset manually. (well unless millis() overflows but i doubt you will use it for that long)

not the best way but probably the simplest. (if you have nothing fancy in mind)

How it works:  (assuming your using a factory arduino 16mhz, with no timers changed)
millis(); reads the amount of time from when your program started.

all the below are equal.
210,000 millis
2100 seconds    1000 millis in a second
3.5 minutes    60 seconds in a minute

So ..while.. millis() ..is greater than.. 3.5 minutes.. do..{nothing}..

Millis goes over the set time and your program gets stuck in a while loop doing nothing.
33  Using Arduino / Programming Questions / Re: LED fading on: April 30, 2011, 12:46:39 am
if (fadeValue != 0){fadeValue = fadeValue-5;}

will keep them dark in your code until the loop breaks

Code:
while(sensorValue <= 15)
  {
     sensorValue = analogRead(sensorPin);
     sensorValue = map(sensorValue,0,1024,0,50);
      
     analogWrite(ledPin11, fadeValue);
     analogWrite(ledPin10, fadeValue);
     analogWrite(ledPin9, fadeValue);
     delay(20);
     fadeValue = 255;   /////////!! Delete this, you are setting fadeValue back to 255 every loop?? Then subtracting 5 in the next line??
                            /////////!!!! you will need to put it in the main loop so it resets when the loop breaks
     fadeValue = fadeValue-5;  //////////////!!!!! This is the line you want to add the if statement to.
     if(fadeValue = 0){ //If the fade reaches 0 (dark),                                       //////////!!!!!  Delete
       analogWrite(ledPin11, 0); //Then stay dark?                                            //////////!!!!!  Delete
       analogWrite(ledPin10, 0); //This doesn't work at all, the LEDs just switch off when    //////////!!!!!  Delete
       analogWrite(ledPin9, 0); //the LDR reads dark, even though fadeValue starts as 255? //////////!!!!!  Delete
     }                                                                            //////////!!!!!  Delete
   }

Adding the if statement makes it so once the fadeValue hits 0, it does not get changed until the loop break

My only guess, why it helps, is that if you -5 when the fadeValue is 0, it starts over at 255.
34  Using Arduino / Programming Questions / Re: LED fading on: April 30, 2011, 12:30:00 am
This code is simple and will fade the led's in order when your sensor falls below 16, and keep them off until the sensor rises above 15.

Code:
int fadeValuePin11 = 255;
int fadeValuePin9 = 255;

void loop()  {

  while(sensorValue <= 15)
  {
     analogWrite(ledPin11, fadeValuePin11);
     analogWrite(ledPin9, fadeValuePin9);
     delay(20);
     if (fadeValuePin11 != 0){
     fadeValuePin11 = fadeValuePin11-5;
     } else {
     if (fadeValuePin9 != 0){
     fadeValuePin9 = fadeValuePin9-5;
     }
     }
    
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue,0,1024,0,50); //Map for LED.
}
  fadeValuePin11 = 255;
  fadeValuePin9 = 255;
  analogWrite(ledPin11, 255);
  analogWrite(ledPin9, 255);
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue,0,1024,0,50); //Map for LED.
}
35  Using Arduino / General Electronics / Re: Piezo Buzzers. Best Option on: April 29, 2011, 11:33:46 pm
I have been reading up on the "watchdog" and its uses. I will be testing the code after i receive my 85's. So for now i have plenty of time to make the code nice and neat.

Thanks Again, I'll post when i see how it all works. And am sure will have plenty more questions once i start working on the 100 other ideas that are bouncing around in my head.
36  Using Arduino / Programming Questions / Re: Code don't do what it should do on: April 29, 2011, 10:22:30 pm
!!!!!!!!!!!!!!!    FINALLY   !!!!!!!!!!!!!!!!!!!!
Finally found your issue, although your code could use some cleaning up it will work if you add a pull-down resister to you input. This has been a big headache lol. Because if I changed your code, not even very much, I could get it to work without the pull-down. I do not know why but your code requires it.

UPDATE
I figured out most the time when i changed the code i would use a debounce in it. So it ignored the noise you are having.

Kind of interesting, when testing your code on a bare uno, i could get the led to flash, although not very brightly, but at the interval set... Just by putting my finger over the input pin..
37  Using Arduino / Programming Questions / Re: Please Help!! on: April 29, 2011, 09:31:20 pm
Something using a short version of blink without delay to print every 2 seconds would be...

Code:
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:
void buzz(int interval)
{
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
  if (buzzerState == 0){
    buzzerState = 128;
  } else {
    buzzerState = 0;}
  analogWrite(buzzer, buzzerState);
}}
38  Using Arduino / Programming Questions / Re: Please Help!! on: April 29, 2011, 08:38:20 pm
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:

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:
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.
39  Using Arduino / Programming Questions / Re: Code don't do what it should do on: April 29, 2011, 11:05:37 am
Here is a fixed breadboard. I tried to leave it as you had it, did mkde it easier to see how the leds are wired. Hope it helps. (only needed wires were put on here)
40  Using Arduino / General Electronics / Re: Piezo Buzzers. Best Option on: April 29, 2011, 02:10:44 am
Now I want to use the 85 even more, but it probably isnt practical for my app. However I am using a tiny13 in a different app, which i am going to replace with a 85. The 13 is powerful enough but i want to learn more about the 85(for other apps in mind) and when buying 25 at a time there is only a $0.21 difference. One app i use the 13 in only uses 78 bytes of mem if i remember right. My external circuit does most the work. When the ignition is on or off the 13 has no power(using no battery). When the ign turns on the chips power is still off, held off by a transistor, then when the ign is turned off the chip powers and holds itself and a relay on(powering a fan and water pump) until it counts away a preset 15 minutes. Then writes output low and is totally off again. If the ign is turned on again, it resets the count by de-powering the chip, until the ign is off again.

Not sure if any of that makes sense, does to me lol.

Works very well, might add thermostat control and/or jumper pin or pot time control, when adding the 85

Back to the previous convo, my 328 uses a output pin to hard reset the chip after user input and eeprom save.(pulls reset pin to low)(which i also forgot about so im up to 7 pins needed again) But if i could software reset the chip, it would be down to 5 I/O's and maybe a 85 would be possible.
41  Using Arduino / General Electronics / Re: Piezo Buzzers. Best Option on: April 29, 2011, 01:32:09 am
I will be running the 85 at 16mhz for ease of transition, from the arduino board.

Just reading the 85's datasheet and am going to use the 328 after all. Using a external clock source will use one of my pins (didnt even think about it, duh me), leaving me with only 5.(I need at least 6) Which is okay, it will allow pins for later additions. Will add some cost though.

So now i just need to find out if the interrupts are affected by the use of tone(). vise-versa
You answered this while i was typing...

The interrupt is only used to silence the buzzer so from what you are saying there should be no conflict in my app.

Code:
void loop{
if (mutepressed == 0){
  attachInterrupt(1, mute, RISING);
playsound();  // Code making buzzer sound
}
}

void mute(){mutepressed = 1; noTone(11); return;} // noTone() will turn buzzer off just in case it happens to be on at moment of interrupt

There is alot more code but that should be enough to show what the interrupt does.

Again thanks for the help, i have spent days, yes days, trying to find most this information. I have learned more from the few replies you have posted than hours of googling, and I am IMO a very good googler. (have used promoted websites to the #1 position more than once)(also have good skills with adwords)(so like to think i can use the otherside of it too)
42  Using Arduino / General Electronics / Re: Piezo Buzzers. Best Option on: April 29, 2011, 12:02:53 am
I just remembered im using an interrupt on pin 3 in my code. Is tone() on pin 11 going to affect this?? Or can i just use tone() on pin 9 or 10 to avoid any possible conflict? Or use interrupt 0 on pin 2??

Also one thing i forgot to ask, if i use tone(11, 2500, 10000); i added a 10 sec duration. Will the code continue to flow or will it delay it until the tone stops? I will test this also so i might have an answer soon on my own.

UPDATE: I tested the second question (with the test code above, but read the hz every sec) and have the answer. The code continues to flow while the tone() is on, at the slow rate, then when the duration runs out, the code speeds back up. So YES my code will continue to run while the tone is playing for the duration set.

Thanks
43  Using Arduino / General Electronics / Re: Piezo Buzzers. Best Option on: April 28, 2011, 11:51:51 pm
Interesting you say this

Quote
For what it's worth, the tone function in the Tiny Core does.

I am planning putting this on a AtTiny85. Now that i have eliminated the need for the boost converter and reduced my project to 6 in/out pins. Sounds like i have more learning todo now....

Again thanks for the advice, it has been very helpful to me, and hopefully others. (From the original posts you can tell how confused I was about this)
44  Using Arduino / General Electronics / Re: Piezo Buzzers. Best Option on: April 28, 2011, 11:38:41 pm
Perfect! Thanks alot, I can order my piezos and continue with this project!

I did some testing with the following code and the results are kind of interesting.

Code:
void loop()                    
{
  digitalWrite(13, HIGH);
  delayMicroseconds(200);
  digitalWrite(13, LOW);
  delayMicroseconds(200);
  hz++;
if (millis() - time >= 20000){
  Serial.print(hz/20);       // prints PWM signal in hz -- 2417hz in this case
  Serial.println("");
  hz = 0;
  time = millis();
}
}
This counts the hz over 20 seconds averages per second and prints it.

This will give a 2417hz reading every time. (probably not totally accurate, because of the time it takes to run code) I then put tone(11, 2500); in the setup(), and it slowed my loop down to 2333hz. Then tried tone(11, 10000); and the reading was 2080.

So the faster i use tone the slower it runs my loop, if i add noTone(); right after Serial.Print, it speeds right back up to the 2417, after it gives the first reading with tone on.

So when tone is on it slows the code down, and the higher the freq used with tone, the more it slow down, turn it off and it speeds right back up.

In my app this doesn't matter but it might help others. Any comments??
45  Using Arduino / General Electronics / Re: Piezo Buzzers. Best Option on: April 28, 2011, 10:35:32 pm
Thanks for the reply!!! This is driving me nuts!

Quote
PWM always runs at one of two frequencies (~977 Hz for timer 0; about half that for the other timers).

Doesnt this say I have 7 choices for PWM on Timer2 ??? Not meant to be rude, just asking.
http://www.arduino.cc/playground/Main/TimerPWMCheatsheet

Quote
tone and PWM use different incompatible modes for the timer.  tone uses a combination of prescaler and OCR value to generate an interrupt at the desired frequency.

Will using tone() keep the buzzer on and let the rest of my code flow fine until i use notone() to turn it off?? That is why I asked about the analogWrite(11, 128); because it would put out the PWM until i called digitalWrite(11, LOW);

Can you verify the first two things are correct assumptions?
Pages: 1 2 [3] 4 5