C# to arduino

Hi, I’m using arduino to activate an adxl345 accelerometer.

This is what i’ve written in C# program but i need extra help from any kind soul. i need the exact the same spects in arduino language… Somehow my C## program oso have some error…

if (254<x<294 && -20<y<20 && -51<z<-11)
listBox1.Items.Add(“It has moved inwards”);

else if (234<x<274 && 7<y<47 && -94<z<-54)
listBox1.Items.Add(“It has moved outwards”);

else if (0<x<40 && 253<y<293 && -52<z<-12)
listBox1.Items.Add(“It has moved right side”);

else if (-24<x<16 && -271<y<-231 && -42<z<22)
listBox1.Items.Add(“It has moved left side”);
else
MessageBox.Show(“Pls try again.”);

I hope someone can help me as i’m getting close to my project dateline… :frowning:

Im assuming you are trying to debug as to why your C# application doesnt work by interfacing with the device directly from code running on the Arduino.

These might be good starting points;

http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html

http://www.sparkfun.com/commerce/product_info.php?products_id=9156

okok… i think i need to be more specific in my question.
My question is:
how do i write this sentence in arduino language?

if (254<x<294 && -20<y<20 && -51<z<-11)

I’ve gotten the program running now. All i need is to insert this line into arduino to output a sentence…

So which means, given the range of x,y and z… it’ll be able to tell which direction it moved.

how to write IF x is within range of 254 to 294
at the same time y is within oso with the range of -20 to 20
together with z is within -51 to -11
den it’ll output a sentence…

I noe my question is stupid but im really weak in programming but i need it done… sorry guys.

Incidentally, I wonder why the string in the line MessageBox.Show("Pls try again."); is abbreviated in such a manner? Did the display device suddenly shrink, or is the output now being sent expensively by telegram?

Thanks for the code. I’ve this code done by myself. if im wrong anywhere, pls let me noe… :slight_smile:

void setup()
{
Serial.begin(9600);
int x,y,z
// delay(2000);

switch(var)
{
case 1:
if((254 < x < 294) && (-20 < y < 20) && (-51 < z < -11))
Serial.println(“It moved inwards”);
break;

case 2:
if ( (234<x<274 && 7<y<47 && -94<z<-54) )

Serial.println(“It moved outwards”);
break;

case 3:
if ( (0<x<40 && 253<y<293 && -52<z<-12)

Serial.println(“It moved right side”);

break;

case 4:
if ( (-24<x<16 && -271<y<-231 && -42<z<22)

Serial.println(“It moved left side”);

default:
Serial.println(“try again”);
}
}

theres an error saying:
sketch_nov08c.cpp: In function ‘void setup()’:
sketch_nov08c:6: error: expected initializer before ‘switch’

may someone give me some pointers. thx

Hi Jean

1) Where do you want to end your void setup() ? a "}" is missing.

2) You have no void loop() in your program!

-Fletcher

thats a problem. cos when i add in void loop before switch(var), alot more errors appear when verifying.

No_.cpp: In function 'void setup()': No_:4: error: expected initializer before '}' token No_.cpp: In function 'void loop()': No_:8: error: 'var' was not declared in this scope No_:10: error: 'x' was not declared in this scope No_:10: error: 'y' was not declared in this scope No_:10: error: 'z' was not declared in this scope No_:12: error: break statement not within loop or switch No_:14: error: case label '2' not within a switch statement No_:15: error: 'x' was not declared in this scope No_:15: error: 'y' was not declared in this scope No_:15: error: 'z' was not declared in this scope No_:18: error: break statement not within loop or switch No_:20: error: case label '3' not within a switch statement No_:21: error: 'x' was not declared in this scope

i did not edit anything.. same program as e one above but once i added in void loop.. these errors came out..but without void loop theres only 2 errors.

Hi Jean

1) Please show your code.

2) You miss a ";" at the end of this line:

int x,y,z

3) How is x, y, z data updated?

4) "var" is not declared and you don't calculate the value. Why are you using a switch at all? Why not go with 4 times if as Richard showed?

5) In case 4 you are missing a "break;"

-Fletcher

   if ((254 < x < 294) && (-20 < y < 20) && (-51 < z < -11) )

You can’t do comparisons like this. Each comparison must stand on its own:

if((254 < x && x < 294) && (-20 < y && y < 20) && (-51 < z && z < -11))
void setup() {
   Serial.begin(9600);
}

void loop(){
int x,y,z
// delay(2000);
switch(var) {
case 1: 
if((254<x && x<294) && (-20<y && y< 20) && (-51<z && z< -11))
     Serial.println("It moved inwards");
break;

case 2:
if ( (234<x && x<274) && (7<y && y<47) && (-94<z && z<-54) )
     Serial.println("It moved outwards");
break;

case 3:
if ( (0<x && x<40) && (253<y && y<293) && (-52<z && z<-12))
     Serial.println("It moved right side");
break;

case 4:
if ( (-24<x && x<16) && (-271<y && y<-231) && (-42<z && z<22))
     Serial.println("It moved left side");
break;

default:
     Serial.println("try again");
break;
}
}

thanks guys! i’ve edited and now left w 2 errors.

No_.cpp: In function ‘void loop()’:
No_:7: error: expected initializer before ‘switch’

int x,y,z

shouldn't that be

int x,y,z;

and you also need to define "var"... When I did two changes, it compiled for me.