Machine


I have a moment to work on the machine, as the steam pipes that supply my place of employment with power have temporarily been interrupted.

I have succeeded in ordering the brain to move the arm back and forth. My machine moves more smoothly in one direction than in the other – it seems that it is destined to be an awkward, misshapen thing. But it shall serve my purpose, or be destroyed.

Now I must consider the details of what thoughts this brain is to think. I wish to be able to move the arm back and forth on command, and to have the brain move the arm itself when the event occurs.

Of particular importance is that the arm must not move too far, lest it damage the device it is attached to. I believe I need to equip it with the following:

  • An arming switch. When the device is armed, then at the event it will move the arm to its extremity.
  • A pair of buttons, for moving the arm forth and back. Perhaps I may be able to have the brain recognise a press-and-hold to differ from a swift double press.
  • A way to inform the device that the current position of the arm is an extremity that it must not move beyond. Perhaps the pair of buttons may serve this purpose: a switch shall indicate that the pair of buttons is to be treated as an order to move the arm; or that they are to be treated as a setting of the arm’s limits.
  • If the buttons can distinguish between a press-and-hold and a double-press, then a press of both buttons will suffice to indicate where the arm is to be moved to at the occurrence of the event.

In order to make any of this occur, I must first equip the machine with a kinesthetic sense – some way to know where the arm is. I shall resort to a lodestone and magnetic switch located on the middle shaft. The brain shall be prodded as the switch activates in the presence of the lodestone. (Will it interpret this prodding as “pain”? Perhaps. Its suffering is unimportant to me, or to science.)

But how will it know whether the count is to be increased or decreased? There are two ways. Two magnetic switches may be used, slightly offset. But a simpler method is for the counter neurons to be aware of the motor neurons.

So. Two switches, two buttons, and the magnetic switch. And perhaps a temporary switch whose task it is to simulate the occurence of the event to which the creat- the machine shall respond.

volatile byte increment = 0;
volatile int position = 0;

int positionA = 0;
int positionB = 0;
int sunrisePosition = 0;

void reed_switch_interrupt() {
  position += increment;
}

void start_up() {
  increment = 1;
  // start the motor
}

void start_down() {
  increment = -1;
  // start the motor
}

void stop() {
  // stop the motor
  increment = 0;
}

void motorTo(int moveTo) {
  long stop_time = now() + 10000; // 10 sec

  if(moveTo > position) {
     start_up();
     while(now()<stop_time && position<moveTo) {
       sleep(100);
     }
  }
  else if(moveTo < position) {
     start_down();
     while(now()moveTo) {
       sleep(100);
     }
  }

  stop();
}

loop() {
  if(need_to_handle_sunrise()) {
    handle_sunrise();
  }
  if(either_button_pressed()) {
    handle_button_press();
  }
  sleep(250);
}

handle_sunrise() {
  moveTo(sunrisePosition);
}

handle_button_press() {
  // ok. watch A||B. if it goes low during the .5 seconds, note the fact
  // after the .5 seconds is done, look at the state of the buttons.
  // this means that a double click is a click then a click-hold

  sleep(25); // debounce

  boolean doubleClick = false;

  long tt = now() + 500;

  while(now() < tt) {
    doubleClick = doubleClick || !either_button_pressed();
  }

  if(!either_button_pressed()) return; // meh.

  if(set_switch_setting()) {
    if(both_buttons_pressed()) {
      sunrise_position = position;
    }
    else if(button_a_pressed()) {
      positionA = position
    }
    else if(button_b_pressed()) {
      positionB = position
    }
  }
  else {
    if(both_buttons_pressed()) {
      motorTo(sunrisePosition);
    }
    else if(button_a_pressed()) {
      if(double_click) {
        motorTo(positionA);
      }
      else {
        start_up();
        while(button_a_pressed()) sleep(10); // maybe put a timer in here, too
        stop();
        positionA = position;
      }
    }
    else if(button_b_pressed()) {
      if(double_click) {
        motorTo(positionB);
      }
      else {
        start_down();
        while(button_b_pressed()) sleep(10); // maybe put a timer in here, too
        stop();
        positionB = position;
      }
    }
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: