Show Posts
Pages: [1]
1  Using Arduino / Motors, Mechanics, and Power / Re: Powering 3 servos & what's everyone using on their workbench? on: March 03, 2011, 03:05:27 pm
Voltages below 20v are pretty safe, and unless the transformer is home-made or ripped open it is safe too.
2  Using Arduino / Programming Questions / Re: Millis, repeat code. how to make it work????? on: March 03, 2011, 02:57:00 pm
I have written a library that should help with this.
Try something like this:
Code:
#include <"Timing.h">

class StateMachine
{
  int state = 1;
  public:
  void next()
  {
    switch(state){
    case 1: digitalWrite(13,HIGH); events.add(1000,next); break;
    case 2: digitalWrite(13,LOW); events.add(1000,next); break;
    default: state = 1;}
    state++;
  }
};

// You also need some code that uses this class

For example the for loop would be replaced with:
Code:
if(state < 4){something; state++;}

EDIT: You posted while I was writing. Use this code if you add another thing with delays.
3  Development / Other Software Development / Re: Parsing PHP on: February 27, 2011, 03:57:46 pm
Try python urrllib. It should be pretty easy with that.
4  Using Arduino / Programming Questions / Re: Toggling Power On and Off on: February 27, 2011, 03:47:19 pm
Amplify the pin current with a transistor.
5  Topics / Home Automation and Networked Objects / Re: Project: Inventory management & object dispencer : Redux on: February 27, 2011, 03:44:50 pm
I' d recommend using linear servos(an arduino uno can control up to 8 of them with the servo library), but using DC motors is really easy:
|-------------------|
PIN                 base
5v----------col-   T
GND       lector   emitter
|--------MOTOR------|
bad image...
Code:
setup: pinMode(pin,OUTPUT);
to control motor: analogWrite(pin,0-1023);
For reversing the motor use this circuit:

If you want to have a free running option, put the NPNs on top and the PNPs down.
6  Development / Other Software Development / Timing library on: February 27, 2011, 03:14:37 pm
I am going to program a robot, so I need to do many things that include lots of delays at once. That's why I made a library that lets you use non-blocking delays that call a function on timeout. It's not as good as threading, but it's very useful in combination with finite state machine classes.

I'm a C++ beginner, but I'm very good at python. I first tried to make the event array change size, but it was hopeless(and would have used up too much cpu.). I'm still trying to figure out how to set the size of the array in the constructor. All improvements are welcome.

Heres the code: (put it in libraries - Timing)
Code:
/*
  Timing.cpp - Non-blocking delay library.
  Created by Joonatan Saarhelo, February 27, 2011.

  Example:

#include <Timing.h>
Timer events;

boolean last = 0;

void setup()
{
  pinMode(13, OUTPUT);
  events.add(1000,set);
}

void loop()
{
  events.update();
}

void set()
{
  last = last == false;
  digitalWrite(13, last);
  events.add(1000,set);
}
*/

#include "WProgram.h"
#include "Timing.h"
#include "functionpointer.h"

Timer::Timer()
{
  length = 0;
  lastcheck = millis();
}

void Timer::add(int in, fpointer func)
{
  events[length] = Event();
  events[length].function = func;
  events[length].timeout = millis()+in;
  length++;
}

void Timer::update()
{
  long time = millis();
  boolean overflow = time < lastcheck;
  for(int se=0; se < length; se++)
  {
    if(events[se].timeout < time or overflow and events[se].timeout > lastcheck)
    {
      events[se].function();
      // siirrä taaksepäin
      for(int i=se+1; i < length; i++) {events[i-1] = events[i];}
      length -= 1;
    }
  }
}
Code:
/*
  Timing.h - Non-blocking delay library.
  Created by Joonatan Saarhelo, February 27, 2011.
 
  Example:

#include <Timing.h>
Timer events;

boolean last = 0;

void setup()
{
  pinMode(13, OUTPUT);
  events.add(1000,set);
}

void loop()
{
  events.update();
}

void set()
{
  last = last == false;
  digitalWrite(13, last);
  events.add(1000,set);
}
*/

#ifndef AHolemassa
#define AHolemassa

#include "WProgram.h"
#include "functionpointer.h"

class Timer
{
struct Event
{
fpointer function;
long timeout;
};
Event events[10];
  int length;
  long lastcheck;
  public:
    Timer();
    //Timer(int maxevents);
    void add(int in, fpointer func);
    void update();
};

#endif
Last and least: functionpointer.h
Code:
typedef void (*fpointer)();

If my english is strange it's because I'm from Finland.
Pages: [1]