Show Posts
Pages: 1 ... 61 62 [63] 64 65 ... 151
931  Using Arduino / Project Guidance / Re: Clock (Hours and Mins) Project on: December 02, 2012, 09:03:08 am
Quote
The clock consists of digits 1 to 12 being lit up (the hours), and 00 to 55 being lit up (the minutes), the board would be controlling 24 relays to turn on a much bigger power supply to control the lights. The relays are latching so I would have to pulse them rather than keep them on.

Using an interrupt and you are done. I wrote the following piece for some other purpose but it can be easily repurposed here:

Code:
//unsigned long _timer1_period=0;  //timer period, in seconds
unsigned long _timer1_counter=0;  //timer1 counter
struct {
  unsigned char hour;
  unsigned char minute;
  unsigned char second;
} mytime;

//timer1 isr
ISR(TIMER1_OVF_vect) {
  _timer1_counter+=0x10000ul;   //increment timer_counter
  if (_timer1_counter >=F_CPU) {
    _timer1_counter -= F_CPU;    //reset timer_counter
    mytime.second+=1; //increment the seconds
    if (mytime.second >=60) {
      mytime.second -= 60; //reset seconds
      mytime.minute += 1; //increment the minute
      if (mytime.minute>=60) {
        mytime.minute -= 60;  //reset minute
        mytime.hour += 1;
      }
    }
  }
}

//initialize the timer
//to expire in user-specified period of time
void tmr1_init(void) {
  _timer1_counter = 0;            //reset timer1 counter
  mytime.hour = mytime.minute = mytime.second = 0;//reset mytime
  TCCR1B = 0x00;                //turn off the timer
  TCCR1A = 0x00;                //normal portion operation
  TCNT1 = 0;                    //reset the timer
  TIFR1 |= (1<<TOV1);            //clear the timer flag
  TCCR1B = 0x01;                //turn on the timer, 1:1 prescaler
  TIMSK1 |= (1<<TOIE1);        //enable the timer interrupt
  sei();                      //enable global interrupt
}

In your code, you will just set it up and test it:
Code:
void setup(void) {
  ...
  tmr1_init(); //reset the timer and get it started
}

void loop(void) {
  if (mytime is mydesiredtime) {
    //do something
  }
}
932  Using Arduino / General Electronics / Re: Constant current power supplies on: December 02, 2012, 08:54:39 am
Quote
find a tiny, cheap, constant voltage power supply that isn't already stuck inside a wall wart to experiment with.

The simplest would be your cell phone / usb chargers. Buy one and break it apart.
933  Using Arduino / Programming Questions / Re: Use momentary to trigger output for a time w/o using delay? on: December 02, 2012, 08:40:55 am
This is something that I would do:

Code:
unsigned long _timer1_period=0;  //timer period, in seconds
unsigned long _timer1_counter=0;  //timer1 counter
//timer1 isr
ISR(TIMER1_OVF_vect) {
  _timer1_counter+=0x10000ul;   //increment timer_counter
  if (_timer1_counter >=F_CPU) {
    _timer1_counter -= F_CPU;    //reset timer_counter
    _timer1_period -= 1;            //decrement duration_sec
    if (!_timer1_period) {        //elapsed time has been reached
      //turn off the led
      digitalWrite(LEDPin, LOW);
      TIMSK1 &=~(1<<TOIE1);      //turn off timer1 interrupt
    }
  }
}

//initialize the timer
//to expire in user-specified period of time
void tmr1_init(unsigned long duration_sec) {
  _timer1_counter = 0;            //reset timer1 counter
  _timer1_period=duration_sec;    //set timer period
  TCCR1B = 0x00;                //turn off the timer
  TCCR1A = 0x00;                //normal portion operation
  TCNT1 = 0;                    //reset the timer
  TIFR1 |= (1<<TOV1);            //clear the timer flag
  TCCR1B = 0x01;                //turn on the timer, 1:1 prescaler
  TIMSK1 |= (1<<TOIE1);        //enable the timer interrupt
  sei();                      //enable global interrupt
}

The isr keeps track of time elapsed and at the end, turns itself off. The other routine just initialize the timer. Once it is set, it goes on by itself.

Here would be  your user code:

Code:
    if (buttonState==HIGH) {
      digitalWrite(LEDPin, HIGH);          //turn on the led pin
      tmr1_init(5);                       //initialize the timer to elapse in 5 seconds
    }

As the routines are set-and-forget type, your user code is greatly simplified: all it does is to set up the timer and then it moves on.
934  Using Arduino / General Electronics / Re: Checking for 12 Volts on: December 02, 2012, 08:37:19 am
Quote
is this a good approach for solving the problem?

It matters a lot if you want to detect the presence of a voltage, or you want to detect the presence of 12v.

In the 1st case, the simplest would be to stuck a resistor (10k for example) between the H_12v line and your mcu pin.

This approach has its drawbacks: it doesn't sense a particular voltage and it is sensitive to interference. You can then use a pull-down resistor to alleviate that. a 47k pull down is fairly good but I typically use a 22k pull down. With that, it produces a 1->0 when the input voltage is less than 1v * (22k+10k)/22k and 0-> when the input voltage is greater than 4v * (22k + 10k) / 22k. So by adjusting the value of the 10k / 22k resistors, you can get it to trip at 12v.
935  Using Arduino / Programming Questions / Re: Struggling with switching Arduino and Random numbers on: December 02, 2012, 08:31:01 am
Quote
The job is fairly simple really. To create a random LED (randomly 1 0f 9 possibles) light for an amount of time being determined by an external timer. When the external timer turns off and comes back on again the Arduino is to generate a new random LED.

It is fairly simple:

1) When the timer comes back on, generate a random number - 8-bit since you are driving 8 leds;
2) Turn on/off the leds to reproduce the pattern. You can assign each bit to represent the 8 leds.
Code:
  ranNumber = generate_random(); //generate a random number
  digitalWrite(LED1, ranNumber & 0x01); //LED1 is bit 0 
  digitalWrite(LED2, ranNumber & 0x02); //LED1 is bit 1
  ...
  digitalWrite(LED8, ranNumber & 0x80); //LED8 is bit 7

That's it.

You can then focus on getting the random number seeded and generated.
936  Community / Bar Sport / Re: Where to get a Due or Raspberry Pi for Xmas gift on: December 01, 2012, 09:55:35 pm
Quote
My grandson won't have to share the family PC any more!

Maybe he can emulate windows on the pi from within linux and use it to program an avr.

smiley
937  Using Arduino / Programming Questions / Re: Use momentary to trigger output for a time w/o using delay? on: December 01, 2012, 09:31:13 pm
Easy:

1) at the time you trigger that output, start a timer to count time.
2) in the timer isr, compare elapsed time vs. desired time.
3) when the desired time has elapsed, turn off the output.
938  Using Arduino / Project Guidance / Re: Understanding the Uno's USB Isolation Circuit on: December 01, 2012, 09:29:46 pm
Quote
But what if my Vin is a regulated +5V?

That has been answered.

Quote
And why are they comparing the Vin with 3.3V?

You have to ask the Arduino people for sure. The voltage regulator they used isn't an ldo and its drop out voltage ranges from 0.7v (low/no load) - 2v (rated load), so I guess that they thought a 1.6v headroom is adequate.
939  Using Arduino / Programming Questions / Re: Check to see if button state has changed? on: December 01, 2012, 04:44:44 pm
Fairly easy.

Code:
#define BUTTON0_PRESSED 0x01 //if button0 is pressed
#define BUTTON1_PRESSED 0x02 //if button1 is pressed

unsigned char read_buttons(unsigned char button0, unsigned char button1) {
  static unsigned char buttons_prev= 0x00; // previous button
  unsigned char tmp;

  //save the previous value
  tmp = buttons_prev;
  //read buttons
  buttons_prev = (digitalRead(button1)?0x00: BUTTON1_PRESSED) | (digitalRead(button0)?0x00:BUTTON0_PRESSED); //buttons active low
  return buttons_prev ^ tmp; //0x01 is button 0 changed state, 0x02 if button 1 changed state, 0x03 if both changed state
}

You can also detect rising or falling edges from this easily.
940  Using Arduino / Project Guidance / Re: Understanding the Uno's USB Isolation Circuit on: December 01, 2012, 04:31:22 pm
Quote
what the application for such a circuit is?

It detects the presence of Vin and switches power supplies: U1A is a comparator. It takes Vin, divides it by 2 and compares it to 3.3v. If Vin / 2 is greater than 3.3v (aka Vin > 6.6v), U1A outputs 1 and turns off T1, which isolates USBVcc from U2. So U2 is powered by +5v (which hopefully is powered by Vin at this point).

If Vin / 2 is less than 3.3v (aka Vin < 6.6v), U1A outputs 0 and turns on T1, which switches in USBVcc.

So 6.6v is the cut off point for Vin to power the device.

They could have done a better job around U1A.
941  Using Arduino / General Electronics / Re: Constant current power supplies on: December 01, 2012, 04:19:28 pm
Quote
I just have to do this:

It is a process thing: you will learn to talk to those whom you can talk to and don't talk to those whom you cannot talk to.

942  Using Arduino / Sensors / Re: water proof temp sensors do they exist? on: December 01, 2012, 03:47:42 pm
What about a truly wireless temperature module, utilizing wireless charging and wireless transmission?

It can be entirely encapsulated into one module, read from and write to wireless.
943  Using Arduino / General Electronics / Re: Constant current power supplies on: December 01, 2012, 03:12:41 pm
Straight out, it is unlikely something like this can be used for a device needing a constant voltage.

The topology, however, is likely a fly-back smps - assuming that it is isolated. That means the led string is floating above a current sampling resistor that controls the output voltage to maintain that constant current (thus constant voltage over the current sampling resistor - likely via a zener + optocoupler).

So all you need to do is to identify that resistor and put in place a divider. That resistor is very easy to identify: it must be between the cathode (led-) and the ground.

After that, you have turned this constant current regulator to a constant voltage regulator.
944  Using Arduino / Microcontrollers / Re: Advantage of Arduino on: December 01, 2012, 02:30:38 pm
Quote
why bother with the added headache and limitation of simulators?

A lot.

For example, I travel a lot. Blinking leds, naked pcb boards and 7-segment leds are huge attractions going through the various "check points" on my journey. Plus, I do tons of cross-platform development it isn't always feasible to have everything with me.

Thus, simulation is vital to me. And it has been a great productivity enhancer, as long as you understand what they cannot do so you can use them for what they can do.
945  Community / Bar Sport / Re: Where to get a Due or Raspberry Pi for Xmas gift on: December 01, 2012, 02:20:59 pm
Quote
I believe the RPi is a tad cheaper

It is.

Quote
and besides it's already on the way.

Roku is available right now, no waiting required.

Quote
Failing to see any similarities

Look a little bit deeper under the skin.
Pages: 1 ... 61 62 [63] 64 65 ... 151