Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

need help in codes thread!
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: need help in codes thread!

by gr33nhorn on Sat Feb 07, 2009 2:27 pm

im back with new problem
Code: Select all | TOGGLE FULL SIZE
#include <math.h>
#define LastID 11
#define Header 0xff 
#define Torque 4

byte PosArray[]={/* ID 0, 1, 2, 3, 4, 5,  6,  7,  8,  9,  10, 11 */
                       5,20,40,60,80,100,120,140,160,180,200,220
                };

byte *PosPtr;

void setup()
  {
    Serial.begin(115200);
    PosPtr=PosArray;
    SyncPosSend(LastID,Torque,*PosPtr);
  }
 
void loop()
{
 
}

void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,HEX);
  Serial.print((Torque<<5)|0x1f,HEX);
  Serial.print(LastID+1,HEX);
    for (i=0; i<=3; i++)
      {
        Serial.print(*(PosPtr+i),DEC);
        Checksum ^=*(PosPtr+i);
      }
     
  Checksum &=0x7f;
  Serial.print(Checksum,HEX);
 
}




error :
Code: Select all | TOGGLE FULL SIZE
error: expected ',' or '...' before numeric constant In function 'void setup()':
 At global scope:


can somebody enlightened me?

thanks in advance
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by gr33nhorn on Sat Feb 07, 2009 3:31 pm

solved!
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by niksun on Sat Feb 07, 2009 4:15 pm

What was the solution?
Niksun

If you thought before that science was certain--well, that is just an error on your part. -- Richard Feynman
niksun
 
Posts: 202
Joined: Sun Jul 27, 2008 9:59 pm

Re: need help in codes thread!

by gr33nhorn on Sun Feb 08, 2009 2:19 am

Code: Select all | TOGGLE FULL SIZE
#include <math.h>

#define Header 0xff

byte PosArray[]={/* ID 0, 1, 2, 3, 4, 5,  6,  7,  8,  9,  10, 11 */
                       5,20,40,60,80,100,120,140,160,180,200,220
                };

byte *PosPtr;

void setup()
  {
    byte Torque, LastID;
    Serial.begin(115200);
    Torque= 4;
    LastID =11;

    PosPtr=PosArray;
    SyncPosSend(LastID,Torque,PosPtr);
  }
 
void loop()
{
 
}

void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,HEX);
  Serial.print((Torque<<5)|0x1f,HEX);
  Serial.print(LastID+1,HEX);
    for (i=0; i<=3; i++)
      {
        Serial.print(*(PosPtr+i),DEC);
        Checksum ^=*(PosPtr+i);
      }
     
  Checksum &=0x7f;
  Serial.print(Checksum,HEX);
 
}



try to compile and see
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by niksun on Sun Feb 08, 2009 10:03 am

You had to declare Torque and LastID as variables rather than constants?
Niksun

If you thought before that science was certain--well, that is just an error on your part. -- Richard Feynman
niksun
 
Posts: 202
Joined: Sun Jul 27, 2008 9:59 pm

Re: need help in codes thread!

by gr33nhorn on Sun Feb 08, 2009 12:01 pm

gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by mtbf0 on Sun Feb 08, 2009 12:49 pm

there is nothing wrong with passing defined constants to your subroutine. the problem is that the preprocessor replaced the instances of Torque and LastID throughout your sketch to the strings "4" and "11". the scope of a define is anything that follows it in the source module until it is redefined or undefined.

you could have also fixed it insanely but effectively like this
Code: Select all | TOGGLE FULL SIZE
#include <math.h>
#define LastID 11
#define Header 0xff
#define Torque 4

byte PosArray[]={/* ID 0, 1, 2, 3, 4, 5,  6,  7,  8,  9,  10, 11 */
                       5,20,40,60,80,100,120,140,160,180,200,220
                };

byte *PosPtr;

void setup()
  {
    Serial.begin(115200);
    PosPtr=PosArray;
    SyncPosSend(LastID,Torque,*PosPtr);
  }
 
void loop()
{
 
}

#undefine Torque
#undefine LastID

void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,HEX);
  Serial.print((Torque<<5)|0x1f,HEX);
  Serial.print(LastID+1,HEX);
    for (i=0; i<=3; i++)
      {
        Serial.print(*(PosPtr+i),DEC);
        Checksum ^=*(PosPtr+i);
      }
     
  Checksum &=0x7f;
  Serial.print(Checksum,HEX);
 
}

a couple of common coding conventions might have helped here.i

defined symbols named with all caps.

first character of a variable name is lower case.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: need help in codes thread!

by gr33nhorn on Mon Feb 09, 2009 12:01 pm

there is an error for this code.what had happened?
Code: Select all | TOGGLE FULL SIZE
2: error: invalid preprocessing directive #undefine In function 'void setup()':
 At global scope:
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by mtbf0 on Mon Feb 09, 2009 9:21 pm

sorry.
Code: Select all | TOGGLE FULL SIZE
#undef Torque
#undef LastID
must remember to wake up fully before posting.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: need help in codes thread!

by gr33nhorn on Tue Feb 10, 2009 11:54 am

hmm..there's still some error.
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by franklin97355 on Wed Feb 11, 2009 12:22 am

hmm..there's still some error.

Then you still have a problem. A more informative post would have included the error message ang where in the code it occured.

franklin97355
 
Posts: 21500
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: need help in codes thread!

by mtbf0 on Wed Feb 11, 2009 9:14 am

perhaps i wasn't clear. when i said that using #undef in this situation was a way to fix your problem [i]insanely[/], what i meant was that it was not the optimal solution or even, you know, a particularly good one.

when i later mentioned naming standards, that's the thing i was actually recommending.

when you use the define directive you are telling the c preprocessor, (not the compiler), to replace every occurrence of the defined name, so by the time the compiler got hold of your function definition it was no longer compilable as c.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: need help in codes thread!

by gr33nhorn on Wed Feb 11, 2009 2:04 pm

hmm..okay..let me digest fully :)
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by gr33nhorn on Sat Feb 14, 2009 7:49 am

Code: Select all | TOGGLE FULL SIZE
#include <math.h>
#define Header 0xff 


byte *PosPtr;
byte ZeroPosArray[]={/* ID 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,   9,  10 ,  11 */
                          10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100, 111 , 120
                    };                                                                       //initial positions.
                   
byte WalkPosArray[5][12]={/* ID 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,   9,  10 ,  11 */
                              {10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100, 110 , 120}, //Index:0 -ViaPoint_0
                              {20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 ,100 , 110, 120 , 130}, //Index:1 -ViaPoint_1
                              {30 , 40 , 50 , 60 , 70 , 80 , 90 ,100 ,110 , 120, 130 , 140}, //Index:0 -ViaPoint_2
                              {40 , 50 , 60 , 70 , 80 , 90 ,100 ,110 ,120 , 130, 140 , 150}, //Index:0 -ViaPoint_3
                              {50 , 60 , 70 , 80 , 90 ,100 ,110 ,120 ,130 , 140, 150 , 160}  //Index:4 -ViaPoint_0
                         };
void setup()
  {
    Serial.begin(115200);
    byte LastID, Torque;
    LastID=11;
    Torque=4; //0~4
    PosPtr=ZeroPosArray;
    SyncPosSend(LastID,Torque,PosPtr);//initialise
   
   
     
  }
 
void loop()
{
}
void SyncPosSend(byte LastID, byte Torque ,byte *PosPtr)
{
  byte i, Checksum;
  i=0;
  Checksum=0;
  Serial.print(Header,BYTE);
  Serial.print((Torque<<5)|0x1f,BYTE);
  Serial.print(LastID+1,BYTE);
    for(i=0; i<=LastID; i++)
      {
        Serial.print(*(PosPtr+i),DEC);
        Checksum ^=*(PosPtr+i);
      }
     
  Checksum &=0x7f;
  Serial.print(Checksum,BYTE);
 
}



i have added a 2dArray and is intended to access it after acessed zeroposarray[], so how am i going to access the multi array if i'm going to use the SyncPosSend() function to access it too?please enlighten me.
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: need help in codes thread!

by gr33nhorn on Wed Apr 08, 2009 12:18 pm

My question on C....say...i have a 2d array of [3][4].......how can i use the values in this array and dump into a function whereby it will calculate the average value in [0][1] and [0][2] and put back into a new 2d array?
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Please be positive and constructive with your questions and comments.