07-05-2016, 19:18
Potrzebuję kodu do sterowania dwoma serwami. Chcę je sterować za pomocą krzyżaka w aplikacji na smartfonie. Jednak po mimo szukania w internecie sketch-ów nic nie znalazłem.
Ocena wątku:
Sketch servo przyciski
|
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
#include <Servo.h>
Servo SerwoH;
Servo SerwoV;
int posH = 90;
int posV = 90;
String message;
String strA ;
int A;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
SerwoH.attach(8);
SerwoV.attach(9);
SerwoH.write(90);
SerwoV.write(90);
delay(200);
}
void loop()
{
while (mySerial.available() > 0){
char c = mySerial.read();
message += c; }
if (message.length() > 1){
strA = message.substring(3);
A = strA.toInt();}
if ( A != 0 ){
Serial.print(A);
if ( A == 1 ){ posV = posV + 5; SerwoV.write(posV); }
if ( A == 2 ){ posV = posV - 5; SerwoV.write(posV); }
if ( A == 3 ){ posH = posH + 5; SerwoH.write(posH); }
if ( A == 4 ){ posH = posH - 5; SerwoH.write(posH); }
}
delay(10);
message = "";
A = 0 ;
}
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
int servoV = 8;
int servoH = 9;
int angle;
int pwm;
int posH = 90;
int posV = 90;
String message;
String strA ;
int A;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
pinMode(servoV,OUTPUT);
pinMode(servoH,OUTPUT);
delay(200);
}
void loop()
{
while (mySerial.available() > 0){
char c = mySerial.read();
message += c; }
if (message.length() > 1){
strA = message.substring(3);
A = strA.toInt();}
if ( A != 0 ){
Serial.print(A);
if ( A == 1 ){ posV = posV + 5; }
if ( A == 2 ){ posV = posV - 5; }
if ( A == 3 ){ posH = posH + 5; }
if ( A == 4 ){ posH = posH - 5; }
}
servoVpuls(servoV,posV);
servoHpuls(servoH,posH);
delay(10);
message = "";
A = 0 ;
}
void servoHpuls (int servo, int angle)
{
pwm = (angle*11) + 500;
digitalWrite(servo, HIGH);
delayMicroseconds(pwm);
digitalWrite(servo, LOW);
}
void servoVpuls (int servoV, int angle)
{
pwm = (angle*11) + 500;
digitalWrite(servoV, HIGH);
delayMicroseconds(pwm);
digitalWrite(servoV, LOW);
}
(11-05-2016, 00:55)JasQ napisał(a): Zerknij czy ci to działa ( oba serwa na krzyżaku ) i czy serwa się nie wzbudzają , jak będą to pokombinujemy dalej . BT podpinasz pod piny 10 i 11 , baud 9600 koniecznie !
Kod:#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
#include <Servo.h>
Servo SerwoH;
Servo SerwoV;
int posH = 90;
int posV = 90;
String message;
String strA ;
int A;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
SerwoH.attach(8);
SerwoV.attach(9);
SerwoH.write(90);
SerwoV.write(90);
delay(200);
}
void loop()
{
while (mySerial.available() > 0){
char c = mySerial.read();
message += c; }
if (message.length() > 1){
strA = message.substring(3);
A = strA.toInt();}
if ( A != 0 ){
Serial.print(A);
if ( A == 1 ){ posV = posV + 5; SerwoV.write(posV); }
if ( A == 2 ){ posV = posV - 5; SerwoV.write(posV); }
if ( A == 3 ){ posH = posH + 5; SerwoH.write(posH); }
if ( A == 4 ){ posH = posH - 5; SerwoH.write(posH); }
}
delay(10);
message = "";
A = 0 ;
}
tu masz kod obsługi serwa bez biblioteki nie wzbudza się tak jak na poprzednim kodzie ale sprawdź bo mogę mieć padnięte serwa i walczę z wiatrakami
Kod:#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
int servoV = 8;
int servoH = 9;
int angle;
int pwm;
int posH = 90;
int posV = 90;
String message;
String strA ;
int A;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
pinMode(servoV,OUTPUT);
pinMode(servoH,OUTPUT);
delay(200);
}
void loop()
{
while (mySerial.available() > 0){
char c = mySerial.read();
message += c; }
if (message.length() > 1){
strA = message.substring(3);
A = strA.toInt();}
if ( A != 0 ){
Serial.print(A);
if ( A == 1 ){ posV = posV + 5; }
if ( A == 2 ){ posV = posV - 5; }
if ( A == 3 ){ posH = posH + 5; }
if ( A == 4 ){ posH = posH - 5; }
}
servoVpuls(servoV,posV);
servoHpuls(servoH,posH);
delay(10);
message = "";
A = 0 ;
}
void servoHpuls (int servo, int angle)
{
pwm = (angle*11) + 500;
digitalWrite(servo, HIGH);
delayMicroseconds(pwm);
digitalWrite(servo, LOW);
}
void servoVpuls (int servoV, int angle)
{
pwm = (angle*11) + 500;
digitalWrite(servoV, HIGH);
delayMicroseconds(pwm);
digitalWrite(servoV, LOW);
}
(11-05-2016, 00:55)JasQ napisał(a): Zerknij czy ci to działa ( oba serwa na krzyżaku ) i czy serwa się nie wzbudzają , jak będą to pokombinujemy dalej . BT podpinasz pod piny 10 i 11 , baud 9600 koniecznie !Na pierwszym działa, ale się wzbudza (po mojemu efekt "padaczki". A drugi w ogóle nie działa.
Kod:#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
#include <Servo.h>
Servo SerwoH;
Servo SerwoV;
int posH = 90;
int posV = 90;
String message;
String strA ;
int A;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
SerwoH.attach(8);
SerwoV.attach(9);
SerwoH.write(90);
SerwoV.write(90);
delay(200);
}
void loop()
{
while (mySerial.available() > 0){
char c = mySerial.read();
message += c; }
if (message.length() > 1){
strA = message.substring(3);
A = strA.toInt();}
if ( A != 0 ){
Serial.print(A);
if ( A == 1 ){ posV = posV + 5; SerwoV.write(posV); }
if ( A == 2 ){ posV = posV - 5; SerwoV.write(posV); }
if ( A == 3 ){ posH = posH + 5; SerwoH.write(posH); }
if ( A == 4 ){ posH = posH - 5; SerwoH.write(posH); }
}
delay(10);
message = "";
A = 0 ;
}
tu masz kod obsługi serwa bez biblioteki nie wzbudza się tak jak na poprzednim kodzie ale sprawdź bo mogę mieć padnięte serwa i walczę z wiatrakami
Kod:#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
int servoV = 8;
int servoH = 9;
int angle;
int pwm;
int posH = 90;
int posV = 90;
String message;
String strA ;
int A;
void setup(){
Serial.begin(9600);
mySerial.begin(9600);
pinMode(servoV,OUTPUT);
pinMode(servoH,OUTPUT);
delay(200);
}
void loop()
{
while (mySerial.available() > 0){
char c = mySerial.read();
message += c; }
if (message.length() > 1){
strA = message.substring(3);
A = strA.toInt();}
if ( A != 0 ){
Serial.print(A);
if ( A == 1 ){ posV = posV + 5; }
if ( A == 2 ){ posV = posV - 5; }
if ( A == 3 ){ posH = posH + 5; }
if ( A == 4 ){ posH = posH - 5; }
}
servoVpuls(servoV,posV);
servoHpuls(servoH,posH);
delay(10);
message = "";
A = 0 ;
}
void servoHpuls (int servo, int angle)
{
pwm = (angle*11) + 500;
digitalWrite(servo, HIGH);
delayMicroseconds(pwm);
digitalWrite(servo, LOW);
}
void servoVpuls (int servoV, int angle)
{
pwm = (angle*11) + 500;
digitalWrite(servoV, HIGH);
delayMicroseconds(pwm);
digitalWrite(servoV, LOW);
}
#include <SoftwareSerial.h>
#define in1 5 //L298n Motor Driver pins.
#define in2 6
#define in3 10
#define in4 11
int command; //Int to store app command state.
int button; // Store how many times a button has been pressed.
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}
void loop() {
Button();
if (Serial.available() > 0) {
command = Serial.read();
Stop(); //Initialize with motors stoped.
switch (command) {
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
}
}
}
void Button () {
Serial.print("button = ");
Serial.print(button);
Serial.println(" ");
delay(10);
}
void forward() {
analogWrite(in1, 255);
analogWrite(in4, 255);
}
void back() {
analogWrite(in2, 255);
analogWrite(in3, 255);
}
void left() {
analogWrite(in3, 255);
analogWrite(in1, 255);
}
void right() {
analogWrite(in4, 255);
analogWrite(in2, 255);
}
void Stop() {
analogWrite(in1, 0);
analogWrite(in2, 0);
analogWrite(in3, 0);
analogWrite(in4, 0);
}
(11-05-2016, 10:30)JasQ napisał(a): Pobaw się jak nie będzie działać wklej poszukamy przyczynyTen kod musiałbym połączyć z kodem od serv.