- #include<SoftwareSerial.h>
- SoftwareSerial BT(8,9);
- const int xpin = A0;
- const int ypin = A1;
- const int zpin = A2;
- const int pin = 4;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(xpin,INPUT);
- pinMode(ypin,INPUT);
- pinMode(zpin,INPUT);
- pinMode(pin,OUTPUT);
- BT.begin(38400);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- int x = analogRead(xpin);
- x=((float)x-331.5)/65*9.8;
- Serial.print("x=");
- Serial.println(x);
- int y = analogRead(ypin);
- y=((float)y-331.5)/65*9.8;
- Serial.print("y=");
- Serial.println(y);
- int z = analogRead(zpin);
- z=((float)z-331.5)/65*9.8;
- Serial.print("z=");
- Serial.println(z);
- if(x > 5)
- {
- digitalWrite(pin,1);
- BT.available();
- BT.write("1");
- }
- else
- {
- digitalWrite(pin,0);
- BT.available();
- BT.write("0");
- }
- delay(100);
- }
複製代碼 |