Pages: [1]   Go Down
Author Topic: (Major headache...!) - 1 line code crashes arduino  (Read 686 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im making a drawing arduino, the code is messy (amature programmer) but faily simple...

only problem is when i uncomment either a call to;
drawLine(x,y);
random(x,y);
in the drawBoxP(int x, int y, int size, int count) function and upload the sketch the aruino doesnt hold LED high for 5 seconds but crashes Serial (even tho its not used) and forces TX high instead.....????????;

im sure im missing something simples and messy cooding to blame...

any help would be much apreaciated.....

phil





int data[50][50] = { ########### DATA ARRAY (0-255 int) ###############}


int StepUnit = 30;  

int w= 38*StepUnit;
int h= 28*StepUnit;

int x1= w/2;
int y1= h;

int a1= sqrt(pow(x1,2)+pow(y1,2));
int b1= sqrt(pow((w-x1),2)+pow(y1,2));

// Stepper motor objects: A is left, B is right
Stepper StepperA(motorStepsA, motorPinA1,motorPinA2, motorPinA3,motorPinA4);                                    
Stepper StepperB(motorStepsB, motorPinB1,motorPinB2, motorPinB3,motorPinB4);


void moveTo(int x2, int y2) {

  // Turn the stepper motors to move the marker from the current point (x1, y1) to (x2, y2)
  // Note: This only moves in a perfectly straight line if
  // the distance is the same in both dimensions; this should be fixed, but it works well

  // a2 and b2 are the final lengths of the left and right strings
  int a2 = sqrt(pow(x2,2)+pow(y2,2));
  int b2 = sqrt(pow((w-x2),2)+pow(y2,2));
  int stepA;
  int stepB;
  if (a2>a1) {
    stepA=1;
  }
  if (a1>a2) {
    stepA=-1;
  }
  if (a2==a1) {
    stepA=0;
  }
  if (b2>b1) {
    stepB=1;
  }
  if (b1>b2) {
    stepB=-1;
  }
  if (b2==b1) {
    stepB=0;
  }

  // Change the length of a1 and b1 until they are equal to the desired length
  while ((a1!=a2) || (b1!=b2)) {
    if (a1!=a2) {
      a1 += stepA;
      StepperA.step(stepA);
    }
    if (b1!=b2) {
      b1 += stepB;
      StepperB.step(-stepB);
    }
  }
  x1 = x2;
  y1=y2;
}

void drawLine(int x2, int y2)
{
  /*Serial.print("DL:");
   Serial.print(x1);
   Serial.print(":");
   Serial.print(y1);
   Serial.print("/");
   Serial.print(x2);
   Serial.print(":");
   Serial.println(y2);*/
  int X = x2-x1;
  int Y = y2-y1;
  int topX = x1;
  int topY = y1;
  int L = (int)sqrt(pow(X,2)+pow(Y,2));
  for (int i = 0; i <= L; i++)
  {
    int x = (int)(X * (double)((i+0.0)/L));
    int y = (int)(Y * (double)((i+0.0)/L));
    moveTo(x+topX,y+topY);
  }
}



void drawBoxP(int dbpsx, int dbpsy, int dbpsize, int dbpcount)
{

  int dbps = dbpsize/2;
  int dbpminX = dbpsx-dbps;
  int dbpminY = dbpsy-dbps;
  int dbpmaxX = dbpsx + dbps;
  int dbpmaxY = dbpsy + dbps;


//THESE ARE THE FUNCTION CALLS THAT FORCE CRASH

  // drawLine(dbpminX,dbpminY);
  //  drawLine(dbpminX,dbpmaxY);
  // drawLine(dbpmaxX,dbpmaxY);
  // drawLine(dbpmaxX,dbpminY);
  // drawLine(dbpminX,dbpminY);

  for (int dbpi = 0; dbpi<dbpcount;dbpi++)
  {

//THESE ARE THE FUNCTION CALLS THAT FORCE CRASH

    // blink(20);
    //    int dbptx = random(dbpminX,dbpmaxX);
    //int dbpty = random(dbpminY,dbpmaxY);
    //drawLine(dbptx,dbpty);

  }
}

int blink(int time)
{
  digitalWrite(13,HIGH);
  delay(time);
  digitalWrite(13,LOW);
}


void setup() {
  StepperA.setSpeed(4);                                                              
  StepperB.setSpeed(4);

  // Serial.begin(9600);
  delay(2000);  
  randomSeed(analogRead(0));
}                                                                                      




void loop()
{
  pinMode(13,OUTPUT);

  delay(500);
  digitalWrite(13,HIGH);
  delay(5000);
  digitalWrite(13,LOW);


  int rows = 50;
  int cols = 50;  

  int size = 10;
  int count=0;


  int sX = x1-((cols*size)/2);
  int sY = y1+20;

  int nX = sX;
  int nY = sY;
  int xX = sX+(cols*size);
  int xY = sY+(rows*size);

  drawLine(nX,nY);
  drawLine(nX,xY);
  drawLine(xX,xY);
  drawLine(xX,nY);
  drawLine(nX,nY);


  for (int yi = 0; yi < rows; yi++)
  {
    for (int xi = 0; xi < cols; xi++)
    {
      count++;
      int px = sX+(size*xi);
      int py = sY+(size*yi);
      int i = (data[yi][xi]/5.0);
      drawBoxP(px,py,size,i);
    }
  }  
}
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Please modify your post and put code tags (
  • button) around the code.

Which board are you using?
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 137
Posts: 6805
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
int data[50][50]
This is 5000 bytes of data, which is more than there is space for data on the Arduino (unless you have a MEGA with 1280 or 2560 CPU.)  Neither the Arduino IDE nor the CPU itself detects this sort of error, causing all sorts of strange crash-like behavior.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50841
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

While you are fixing the oversize array, take a look at this statement:
Code:
int L = (int)sqrt(pow(X,2)+pow(Y,2));
and this page:
http://arduino.cc/en/Reference/Pow
The pow() function expects two floats, not two ints. So, a cast is performed to convert the ints to floats, so that pow can effectively do this for you: X * X.

Instead of the casts and the overhead of a relatively inefficient algorithm, just multiply the number by itself.

One last observation. Executable code must be in a function.
Code:
int w= 38*StepUnit;
int h= 28*StepUnit;

int x1= w/2;
int y1= h;

int a1= sqrt(pow(x1,2)+pow(y1,2));
int b1= sqrt(pow((w-x1),2)+pow(y1,2));
None of this code will do what you think it will.
Logged

Pages: [1]   Go Up
Jump to: