12-12-2016, 20:29
To co mi podałeś to ten sam przykład jest w programie Arduino w zakładce przykłady. Korzytałem z niego i nie mam pojęcia jak mam dołączyć to co jest stworzone dla sterowania bluetooth.
Ocena wątku:
Sterowanie serwomechanizmamy Arduino+Bluetooth+Android
|
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo1, myservo2, myservo3, myservo4, myservo5, myservo6;
int bluetoothTx = 12;
int bluetoothRx = 13;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
myservo1.attach(2);
myservo1.write(30); //pozycja wyjściowa serwa obrotu robotem
myservo2.attach(3);
myservo2.write(90); //pozycja wyjściowa serwa ramie 1
myservo3.attach(4);
myservo3.write(90); //pozycja wyjściowa serwa ramie 2
myservo4.attach(5);
myservo4.write(0); //pozycja wyjściowa serwa nadgarstek
myservo5.attach(6);
myservo5.write(0); //pozycja wyjściowa serwa obrót chwytaka
myservo6.attach(7);
myservo6.write(0); //pozycja wyjściowa serwa zamknięcie chwytaka
Serial.begin(9600); //Setup usb serial connection to computer
bluetooth.begin(9600); //Setup Bluetooth serial connection to android
}
void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()>= 2 )
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);
if (realservo >= 1000 && realservo <1180)
{
int servo1 = realservo;
servo1 = map(servo1, 1000,1180,30,150);
myservo1.write(servo1); //serwo obrót ramie 1
Serial.println("servo 1 ON");
delay(30);
}
if (realservo >=2000 && realservo <2180)
{
int servo2 = realservo;
servo2 = map(servo2,2000,2180,30,150);
myservo2.write(servo2); //serwo obrót ramie 1
Serial.println("servo 2 On");
delay(40);
}
if (realservo >=3000 && realservo < 3180)
{
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180,30,150);
myservo3.write(servo3);
Serial.println("servo 3 On");
delay(10);
}
if (realservo >=4000 && realservo < 4180)
{
int servo4 = realservo;
servo4 = map(servo4, 4000, 4180,0,180);
myservo4.write(servo4);
Serial.println("servo 4 On");
delay(10);
}
if (realservo >=5000 && realservo < 5180)
{
int servo5 = realservo;
servo5 = map(servo5, 5000, 5180,0,180);
myservo5.write(servo5);
Serial.println("servo 5 On");
delay(10);
}
if (realservo >=6000 && realservo < 6180)
{
int servo6 = realservo;
servo6 = map(servo6, 6000, 6180,0,180);
myservo6.write(servo6);
Serial.println("servo 6 On");
delay(10);
}
}
}
#include <SoftwareSerial.h>
#include <Servo.h>
int bluetoothTx = 12;
int bluetoothRx = 13;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
byte last_1,last_2,last_3,last_4,last_5,last_6,last_x;
Servo servo1,servo2,servo3,servo4,servo5,servo6;
int pos = 0;
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
servo1.attach(2);
servo2.attach(3);
servo3.attach(4);
servo4.attach(5);
servo5.attach(6);
servo6.attach(7);
}
void loop() {
if(Serial.available()>= 2 ){
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);
if ( realservo >= 1000 && realservo <1180 ){
int x = map(realservo, 1000,1180,30,150);
last_x = last_1;serwo(x,1); last_1 = x; }
if ( realservo >= 2000 && realservo <2180 ){
int x = map(realservo, 2000,2180,30,150);
last_x = last_2;serwo(x,2); last_2 = x; }
if ( realservo >= 3000 && realservo <3180 ){
int x = map(realservo, 3000,3180,30,150);
last_x = last_3;serwo(x,3); last_3 = x; }
}
}
void serwo(int x,byte y){
if ( x > last_x ){
for (pos = last_x; pos <= x; pos += 1) {
if ( y == 1 ){servo1.write(pos);delay(20);}
if ( y == 2 ){servo2.write(pos);delay(20);}
if ( y == 3 ){servo3.write(pos);delay(20);}
}}
if ( x < last_x ){
for (pos = last_x ; pos >= x; pos -= 1) {
if ( y == 1 ){servo1.write(pos);delay(20);}
if ( y == 2 ){servo2.write(pos);delay(20);}
if ( y == 3 ){servo3.write(pos);delay(20);}
}}
}
myservo.write(90);
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
servo1.attach(2);
servo2.attach(3);
servo3.attach(4);
servo4.attach(5);
servo5.attach(6);
servo6.attach(7);
last_x = last_1 = 90;serwo(90,1);
last_x = last_2 = 90;serwo(90,2);
last_x = last_3 = 90;serwo(90,3);
}