variables differ with pulse on digital port 9

hi all

this problem is really weird. my presentation is on wednesday and arduino fools me..

i am driving stepper motors with a pulse on digital port 9:

  //
  // pulse timer setup
  //

  // setup timer clock
  TCCR1A = TCCR1A & 0x0C | 0x50;      // switch output compare to toggle
  TCCR1B = TCCR1B & ~7 | prescaleval; // prescaler clock to /64

  // setup CTC 0 ClearTimerCompare mode
  TCCR1A = TCCR1A & 0xFC;  //clear WGM1(1:0)
  TCCR1B = TCCR1B & 0xEF;  //clear WGM1(3)
  TCCR1B = TCCR1B | 0x08;  //set   WGM1(2)

  /// set frequency
  OCValue = clock / freq;  // darf nicht groesser als 65535 !
  OCR1AH = OCValue >> 8; // write high byte
  OCR1AL = OCValue & 0x00FF; // write low byte

when i am adding this function the input values of my analog ports become random numbers after some simple calculations. widthout calculations they are still fine.

void setFreq(long newFreq)
{
  OCValue = clock / newFreq;
  //debug(OCValue);
  OCR1AH = OCValue >> 8; // write high byte
  OCR1AL = OCValue & 0x00FF; // write low byte
}

any suggestions? my collegue wrote the pulse code. is anything of it conflicting arduino code?

greeting

michael

update: there seems to be a problem with Serial.print(). when i am printing only one value everything works. when i am printing several values the values get weird.

update: everything woks great in arduino 0003. i hope this issue helps you fixing the problem.

Can you post the full code for the non-working sketch? What values do you receive when you run it?

here is the full code. the important part ends with the serial code. the lines after are only logic.

this code works great in version 0003. in version 0004 i can only print “pressure_1_diff” or “pressure_2_diff” at one time. when i print both of them or when i am using them in a calculation the values get weird. one time only values between 200 and 400 were printed (not the analog values of my sensors). another time it only printed random floating values.

#define DOWN 0
#define UP 1
#define ON 0
#define OFF 1

int pressure_1, pressure_2;
int pressure_1_smooth, pressure_2_smooth;
int pressure_1_default, pressure_2_default;
int pressure_1_diff, pressure_2_diff;

int p_pressure_1 = 4;
int p_pressure_2 = 5;

int calibrateTime = 1000;

boolean button_1, button_2;
int p_button_1 = 7;
int p_button_2 = 8;

int p_clock = 2;
int p_direction_1 = 3;
int p_enable_1 = 4;
int p_direction_2 = 5;
int p_enable_2 = 6;

char prescaleval = 3;       // clock / 64 (page 100)
long clock = 16000000 / 64; // set to timer clock = sytem clock / prescaler
long freq = 16000000 / 64;
long OCValue;

int direction, speed;
int mode;


void setup()
{
  //Serial.begin(9600);
  beginSerial(9600);

  // motor pins
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  // button pins
  pinMode(7, INPUT);
  pinMode(8, INPUT);

  // pulse pin
  pinMode(9, OUTPUT);


  //
  // pulse timer setup
  //

  // setup timer clock
  TCCR1A = TCCR1A & 0x0C | 0x50;      // switch output compare to toggle
  TCCR1B = TCCR1B & ~7 | prescaleval; // prescaler clock to /64

  // setup CTC 0 ClearTimerCompare mode
  TCCR1A = TCCR1A & 0xFC;  //clear WGM1(1:0)
  TCCR1B = TCCR1B & 0xEF;  //clear WGM1(3)
  TCCR1B = TCCR1B | 0x08;  //set   WGM1(2)

  /// set frequency
  OCValue = clock / freq;  // darf nicht groesser als 65535 !
  OCR1AH = OCValue >> 8; // write high byte
  OCR1AL = OCValue & 0x00FF; // write low byte

  // neutral mode
  mode = 0;
}

void setFreq(long newFreq)
{
  OCValue = clock / newFreq;  // darf nicht groesser als 65535 !
  OCR1AH = OCValue >> 8; // write high byte
  OCR1AL = OCValue & 0x00FF; // write low byte
}

void set_direction(int dir)
{
  digitalWrite(p_direction_1, dir);
  digitalWrite(p_direction_2, dir);
}

void set_enable(int enable)
{
  digitalWrite(p_enable_1, enable);
  digitalWrite(p_enable_2, enable);
}

void loop()
{

  button_1 = digitalRead(p_button_1);
  button_2 = digitalRead(p_button_2);

  // read pressure sensor
  pressure_1 = analogRead(p_pressure_1);
  pressure_2 = analogRead(p_pressure_2);

  // smoothing
  if (pressure_1 > pressure_1_smooth)
    pressure_1_smooth = pressure_1_smooth + (pressure_1 - pressure_1_smooth)/15;
  else
    pressure_1_smooth = pressure_1_smooth - (pressure_1_smooth - pressure_1)/15;

  if (pressure_2 > pressure_2_smooth)
    pressure_2_smooth = pressure_2_smooth + (pressure_2 - pressure_2_smooth)/15;
  else
    pressure_2_smooth = pressure_2_smooth - (pressure_2_smooth - pressure_2)/15;


  if (millis() < calibrateTime)
  {
    pressure_1_default = pressure_1_smooth;
    pressure_2_default = pressure_2_smooth;
  }

  pressure_1_diff = pressure_1_default - pressure_1_smooth;
  pressure_2_diff = pressure_2_default - pressure_2_smooth;

  //speed = (pressure_1_diff + pressure_2_diff) * 100 + 5000;

  //
  // serial
  //

  //
  // serial out
  //
  
  printInteger(mode);
  serialWrite(59);
  printInteger(pressure_1_diff);
  serialWrite(59);
  printInteger(pressure_2_diff);
  serialWrite(10);
  serialWrite(13);
  delay(10);

  //
  // serial in
  //
  
  while (serialAvailable())
  {
    int serialIn = serialRead();
    if (serialIn != -1)
      mode = (int)serialIn - 1;
  }


  if (button_1 && button_2)
  {
    if (mode == -1)
    {
      if (pressure_1_diff < -55 || pressure_2_diff < -55)
      {
        set_enable(ON);
        direction = DOWN;
        set_direction(direction);
        setFreq(8000);
      }
      else
      {
        set_enable(OFF);
      }
    }
    else if (mode == 1)
    {
      if (pressure_1_diff > -55 && pressure_2_diff > -55)
      {
        set_enable(ON);
        direction = UP;
        set_direction(direction);
      }
      else if (pressure_1_diff < -55 || pressure_2_diff < -55)
      {
        set_enable(OFF);
        direction = DOWN;
        set_direction(direction);
        mode = 0;
      }
    }
    else if (mode == 0)
    {
      set_enable(OFF);
    }
  }
  else if (button_1 && !button_2)
  {
    set_enable(ON);
    direction = DOWN;
    set_direction(direction);
    mode = -1;
  }
  else if (!button_1 && button_2)
  {
    set_enable(ON);
    direction = UP;
    set_direction(direction);
    mode = 1;
  }

  else if (!button_1 && !button_2)
  {
    pressure_1_default = pressure_1_smooth;
    pressure_2_default = pressure_2_smooth;
    set_enable(OFF);
    mode = 0;
  }
}