Go Down

Topic: Euclid, Bjorklund algorithm (Read 980 times) previous topic - next topic

nikitsan

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: [Select]
#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);

      }
  }

gardner

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.

nikitsan

#2
Nov 05, 2011, 10:46 pm Last Edit: Nov 05, 2011, 10:49 pm by nikitsan Reason: 1
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: [Select]
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);

      }
  }

WizenedEE

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

nikitsan

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

Go Up