Show Posts
Pages: 1 ... 18 19 [20] 21 22 ... 24
286  Using Arduino / Programming Questions / Re: Programming traffic light, want multiple loops at random on: January 02, 2013, 09:16:39 am

byte* sequence = {0,2,1,2};
that means "green light, pause, red light, pause".


Since he has "Arrow" lights, I would think he would want cases where he could have more than one on at the time, so maybe

byte sequence[] = { 0b00001, 0, 0b00010, 0, 0b00011, 0};
that means "green light, pause, red light, pause, green and red light, pause...".

Cheers,
John

287  Using Arduino / Programming Questions / Re: LED Light Sensor Circuit Inverted on: January 02, 2013, 09:03:32 am
Hi All

I'm struggling to understand where I'm going wrong with a very simple circuit.

I have light sensitive resistor that is being used to switch on an LED when the light level drops below a fixed value.  I'm monitoring the analogue value returned by the sensor and the value being used to set the LED via the serial window.  The values are as expected so I think the error is in the very simple LED circuit.

Code:
  int val = analogRead(sensePin);
  boolean ledVal = LOW;
  if (val < 750)
  {
    ledVal = HIGH;
  }
  else
  {
    ledVal = LOW;
  }
  digitalWrite(ledPin,ledVal);

  Serial.println(ledVal);

  delay(500);

}

The LED is being switched on and off as the light levels change but the wrong way around; it is on when the ledVal is LOW and off when the ledVal is HIGH.

From what I can tell, val will be great when there is much light, and val will be small when there is little light.  Therefore, the code above will cause the led to light when the ambient light is low.  Is that what it's doing?  If you want the opposite, code
Code:
if (val < 750)
  {
    ledVal = LOW;
  }
  else
  {
    ledVal = HIGH;
  }

Hope this helps!
John
288  Using Arduino / Programming Questions / Re: Too many "if" statements? on: January 02, 2013, 07:37:50 am
Do you know of a way to make

Code:
Udp.write(F("Amaretto Mist- Serve with a lime wedge.  Enjoy!"));
work just like
Code:
Serial.print(F("Amaretto Mist- Serve with a lime wedge.  Enjoy!"));
?
...
ALTERNATIVELY- can the Udp.write send a string instead?  (I can't seem to get it to.)

I think you may be able to write a function in your own sketch something like this:
Code:
   void udpWrite( const __FlashStringHelper *f) {
Udp.beginPacket(iPhoneIP,iPhonePort);
Udp.write((const char *)f);    // Send Message back to iPhone
Udp.endPacket();
   }
and then call it like this
Code:
   kahlua15(); vodka15(); End();
   udpWrite( F("Klingon koffee- Serve on the rocks.  I know... too late!"));

But I'm not sure of the details on this, I have never messed with that technique before.  I'm sure others here have and can help with troubles with it...

John
289  Using Arduino / Programming Questions / Re: Add to an integer on: January 01, 2013, 10:31:29 pm
Or the obscure and unnecessarily complicated:
Code:

  i -=- 1 ;

Love it!  I will have to use this in my next project!!
290  Using Arduino / Programming Questions / Re: Blink Without delay help again. on: January 01, 2013, 10:20:03 pm
...
The constructor will be called before the init() function is called. It is the init() function that sets up the hardware, so calling pinMode() before init() is a waste of time.
...

I thought of that but found that pinMode() was effective.  This was on a Teensy 2 btw,  so I'm not sure if its the same on an Uno, etc.

I suppose it's "undefined" or uncertain whether it should/could/would work or not, so I agree a separate setup() method is best.

Cheers,
John
291  Using Arduino / Programming Questions / Re: Too many "if" statements? on: January 01, 2013, 09:42:31 pm
Looks good Jim!  Where did you get that iOS UI app?

I corrected the functions, paste these instead:
Code:
//////////////////////////////// Warning Light Blink ///////////////////////////

void flashdrink(byte &amt, byte flashcount , int min, int max){
  if ( amt > max) amt = 0;
  if ( amt >= min) {
    flash( flashcount);
    delay( warn);
  }
}

void flashdrink(byte &amt, byte flashcount)
{  flashdrink( amt, flashcount, 180, 190); }

void flash(byte flashcount){
  for (byte x = 0; x <= flashcount; x++)  {
      digitalWrite(LED, 1);
      delay (250);
      digitalWrite(LED, 0);
      delay (250);
  }
}

Then your // W A R N I N G  L I G H T S /// can look like this:
Code:
flashdrink(vodka, 1);
flashdrink(rumdk, 2);
flashdrink(whiskey, 3);
flashdrink(gin  , 4);
flashdrink(brandy, 5);
...
flashdrink(water, 22);

Cheers!
John

P.S. you asked about the &amt. Without it, a copy of the value (e.g. vodka) gets sent to the parameter "amt" in the function.  With the &, a reference to the "location" of where the value is stored, is sent to the amt parameter.  The difference is that reference allows the function code to change the value of "vodka" (reset it to zero).
292  Using Arduino / Networking, Protocols, and Devices / Re: Making internet interaction universal on: January 01, 2013, 05:41:00 pm
The short answer is to forward a port on your router.  What kind of router do you have?

John
293  Using Arduino / Programming Questions / Re: Add to an integer on: January 01, 2013, 05:37:23 pm
Hello I would like to know ad +1 to integer. Does anybody know how to do that?

http://arduino.cc/en/Reference/Arithmetic
294  Using Arduino / Programming Questions / Re: lil help please on: January 01, 2013, 04:52:07 pm
OK I think I got it yay.. here's what I did.
Code:
...

And what happened when you ran it?

John
295  Using Arduino / Programming Questions / Re: Too many "if" statements? on: January 01, 2013, 04:41:41 pm
...
Code:
//begin drink integers

enum { VODKA, RUMLT, RUMDK, WHISKEY, GIN, BRANDY, TEQUILA, AMARRETTO };
byte drinkMix[ 8 ][ 3 ] = { 1,0,0, 2,0,0, 4,0,0, 8,0,0, 16,0,0, 0,2,2, 4,0,0, 32,0,0  };


Only for more than 8 ingredients. You access the parts you want through 2 indexes.
The enum gives you code names for the 1st index, I don't know what the 2nd index is about.

So instead of gin3rd you have drinkMix[ GIN ][ 2 ], 2 as in 0-1-2 not 1st-2nd-3rd. The same array provides for easy-to-switch-between data for all your ingredients.
...

True enough, but I would rather see something like
Code:
struct DrinkPump {
        int id;
const char* name;
int first, second, third;     // rename these better
unsigned int totalpoured;  // qtr-oz
int minamt,maxamt;
    ...
        void checkEmpty();
};

DrinkPump pumps={
  {1,"Vodka",  1,0,0, 180, 190},
  {2,"Tonic",   1,3,0, 180, 190},
  {3,"Tequila", 1,0,7, 360, 380}  // we use these by the "handles" in texas
...
 };

// Aka (I think) "flash count" in previous code
void Pump::checkEmpty( Pump &pump)
{
if (totalpoured > maxamt) {
cout << "Resetting " << name << endl;
totalpoured = 0;
}
if (totalpoured >= minamt) {
cout << "flashing led for " << name << ", " << flashcount << " times" << endl;
flashEmptyLED(id);  // flash the number of times per the id
}
}

So you end up with
     for (int i=0; i< numberofpumps ; i++)
          pumps.checkEmpty();

My 2 pesos smiley

John
296  Using Arduino / Programming Questions / Re: Too many "if" statements? on: January 01, 2013, 04:19:09 pm
John:

Very cool.  I'm beginning to grasp this function-making business now.

I tried your example, and it gave me a "flashdrink not declared in this scope" message when I put the

flashdrink(vodka, 1);


Hi Jim---

It should work, could you attach your code to a message and we can take a look?

John
297  Using Arduino / Programming Questions / Re: Blink Without delay help again. on: January 01, 2013, 03:49:50 pm
ok guys could you all help me to turn this into a class?
heres what i think so far
Code:
class Blink
...

would this be correct ? it does compile but im lost when i get to this part.

Hi Ash, did you not see my reply to your post in "Arduino Class C++"?
http://arduino.cc/forum/index.php/topic,139996.msg1052178.html#msg1052178

John
298  Using Arduino / Programming Questions / Re: Arduino Class C++ on: December 31, 2012, 07:20:36 pm
Hi Ash,

See if this is helpful for you...

Cheers,
John

Code:

class Blinker{
  public:
  Blinker(int pin)
  {
    pinMode(pin,OUTPUT);
    _pin=pin;
    state=0;
    delayms = 100;
  }
  void loop()
  {
    long now = millis();
    switch(state)
    {
    case 0:
      digitalWrite(_pin,HIGH);
      timer=now;
      state = 1;
      break;
    case 1:
      if (now-timer>=delayms)
      {
        state=2;
      }
      break;
    case 2:
      digitalWrite(_pin,LOW);
      timer=now;
      state=3;
      break;
    case 3:
      if (now-timer>=delayms)
      {
        state=0;
      }
    }
  }
  void setDelayMs(long dms){ delayms=dms; }
private:
  unsigned long timer;
  int pin;
  int _pin;
  int state;
  long delayms;
};


Blinker led1(13);  // pin 13
Blinker led2(11);  // pin 11

void setup()
{
  led2.setDelayMs(250);  // led2 will be constant at  2 cycles per second
}

void loop()
{
  // make led1 speed up over the course of 30 seconds
  long delay = 1000 - (millis()/1000)%30 * 30;
  led1.setDelayMs(delay);

  led1.loop();
  led2.loop();
}
299  Using Arduino / Programming Questions / Re: Audioino? on: December 31, 2012, 06:43:43 pm
Quote
end users would not require any extra hardware ...
Apart from an audio player and some wires? And an interface circuit?

Aren't we talking about a potential use-case where the user could reprogram it by playing a wav file on their iOS/Android/laptop and an off-the-shelf 3.5mm patch plug? 

I could see the attractiveness of that in certain situations!
Or maybe I'm missing a "fatal-flaw" here?

John

300  Using Arduino / Programming Questions / Re: Too many "if" statements? on: December 31, 2012, 06:36:19 pm
Quote
define a class for a pump

Do you mean create a library for the pumps, defining classes and such in it?  If so, I tried this out earlier (with the hole file.h, file.cpp, and keywords.txt mumbo-jumbo) and MAN was it confusing.  Even once I got it doing something, I was completely lost as to how to get it to do anything else...

Or did you mean something else?  Can I define a class right inside my sketch?

Quote
idealize the equipment so that you can reuse the same object multiple times.


Yes, but we can get to that later smiley

Quote
...
UKHeliBob:

Oh man!  I was blind and now I see.  I tried out your elegant solution:
Quote
void flash(byte flashcount)
{
  for (byte x = 0; x <= flashcount; x++)
  {
      digitalWrite(LED, 1);
   delay (250);
      digitalWrite(LED, 0);
   delay (250);
  }
...
if (vodka >= 180) {
   
     flash(1);
 
   delay(warn);
    }


and it works like a champ!  Thanks so much!

Welcome to the world of functions!!
In your code you can take this one step further with good effect by adding this:

Code:

void flashdrink(int & amt, byte flashcount, int min=180, int max=190 )
{
  if ( amt > max) amt = 0;
  if ( amt >= min) {
    flash( flashcount);
    delay( warn);
  }
}


 now your calling code can be simply like this:
Code:
  flashdrink(orange, 17);
  flashdrink(pine, 18);
  flashdrink(cola, 19);
  flashdrink(sprite, 20);
  ...
  flashdrink(water, 24, 220, 250);  // or if you find that it needs to be other than 180 and 190

 

Cheers,
John
Pages: 1 ... 18 19 [20] 21 22 ... 24