Pages: [1]   Go Down
Author Topic: Euclid, Bjorklund algorithm  (Read 939 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Love Open Source
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I`m trying to compile the code from Bjorklund`s "The Theory of Rep-Rate Pattern Generation in the SNS Timing System"
https://ics-web.sns.ornl.gov/timing/Rep-Rate%20Tech%20Note.pdf
and have bad results on output.
Can someone lead me in this?
here is the code:
Code:
#include "LiquidCrystal.h"

LiquidCrystal lcd(12,11,5,4,3,2);
int remainder[16];
int count[16];
int divisor;
int i;
int pattern[16];

void setup() {
lcd.begin(16,2);
//lcd.print("ON");
}


void loop() {
compute_bitmap (8,3);

delay(1000);
lcd.clear();

}


void compute_bitmap (int num_slots, int num_pulses)
{
    divisor = (num_slots - num_pulses);
    remainder[0] = num_pulses;
    int newLength;
    int level = 0;
    int cycleLength = 1;
    int remLength = 1;
    do {
        count[level] = (divisor / remainder[level]);
        remainder[level+1] = (divisor % remainder[level]);
        divisor = remainder[level];
        newLength = (cycleLength * count[level]) + remLength;
        remLength = cycleLength;
        cycleLength = newLength;
        level = level + 1; }
    while (remainder[level] > 1);
    count[level] = divisor;
    if (remainder[level] > 0)
      cycleLength = (cycleLength * count[level]) + remLength;
    build_string (level);
}


void build_string (int level)   {

  if (level == -1) {
  //append a “0” to the end of the bitmap; 
  //lcd.print("0"); //try to output on LCD for debug
  }
  else if (level == -2) {
  //append a “1” to the end of the bitmap;
  //lcd.print("1"); //try to output on LCD for debug
  }
  else { for (i=0; i < count[level]; i++)
      build_string (level-1);

      if (remainder[level] != 0)
        build_string (level-2);

      }
  }
Logged

Ontario
Offline Offline
God Member
*****
Karma: 25
Posts: 889
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you explain what is supposed to happen, and what does happen?

I would suggest that you hook up a serial monitor and add some debugging output to your sketch, to help trace what is going on and localize the source of problems.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
Love Open Source
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have LCD for debug messages.
Code of compute_bitmap and build_string I have from PDF in link.
But somthig goes wrong. Code compiles and uploads to arduino uno.
i.e. posted code must print on LCD euclidian pattern with 8 steps and 3 pulses.
it must be 10010010. But it prints 01001.
When I try with 8 steps and 5 pulses it prints many  zeros and ones over and over.
lines for lcd.print must be uncomented.

Code:
void build_string (int level)   {

  if (level == -1) {
  //append a “0” to the end of the bitmap; 
  lcd.print("0"); //try to output on LCD for debug
  }
  else if (level == -2) {
  //append a “1” to the end of the bitmap;
  lcd.print("1"); //try to output on LCD for debug
  }
  else { for (i=0; i < count[level]; i++)
      build_string (level-1);

      if (remainder[level] != 0)
        build_string (level-2);

      }
  }
« Last Edit: November 05, 2011, 04:49:12 pm by nikitsan » Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you want your do...while loop in compute_bitmap to say
Code:
while (remainder[level+1] >= 1)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
Love Open Source
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

any luck with
Code:
while (remainder[level+1] >= 1)
  smiley-confuse
Logged

Pages: [1]   Go Up
Jump to: