# Euclid, Bjorklund algorithm

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:

``````#include "LiquidCrystal.h"

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

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 = 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);

}
}
``````

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.

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.

``````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);

}
}
``````

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

any luck with `while (remainder[level+1] >= 1)` :~