0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #15 on: July 09, 2012, 06:39:35 am » |
But you didn't turn the pin off. how do i do tat sorry for my lack of knowledge in coding
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #16 on: July 09, 2012, 06:41:58 am » |
Like most digital operations, it is the opposite of turning it on, which you did just before the delay
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #17 on: July 09, 2012, 06:51:07 am » |
so should i do this case 'H': digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW);
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #18 on: July 09, 2012, 06:53:48 am » |
That will turn it off after a second, yes, but it won't stop the "delay" making your loop unresponsive. Suppose you've just turned on the horn, then notice you're about to slam into a wall. The loop will not see your emergency stop command until a second later, which could be too late.
See reply #7
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #19 on: July 09, 2012, 07:06:36 am » |
thanks
i got both work now.
next planing to add pan & tilt
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #20 on: July 10, 2012, 07:06:57 am » |
can u plz check what wrong with this code, i added servo pan and tilt code . when i compile theres no error but nothing is working . but when i remove the servo code that i added it works. #include <Servo.h>
Servo xServo; Servo yServo;
int stepSize = 10;
const int xInit = 90; const int yInit = 90; const int xMin = 0; const int xMax = 180; const int yMin = 0; const int yMax = 180;
int x = xInit; int y = yInit; int xNew = x; int yNew = y;
int mA1 = 8; int mA2 = 9; int mB1 = 10; int mB2 = 11;
void setup(){ Serial.begin(9600);
pinMode(mA1, OUTPUT); pinMode(mA2, OUTPUT); pinMode(mB1, OUTPUT); pinMode(mB2, OUTPUT); pinMode(2, OUTPUT); pinMode(13, OUTPUT);
xServo.attach(5); // attaches the servo on pin 5 yServo.attach(6); // attaches the servo on pin 6
delay(1000); MoveServo(xServo, xServo.read(), x); MoveServo(yServo, yServo.read(), y); delay(100);
}
void loop(){ if (Serial.available() > 0) {
char msg = Serial.read();
if (msg == 'C') { xNew=xInit; yNew=yInit; }
if (msg == 'E') xNew = x - stepSize; if (msg == 'W') xNew = x + stepSize;
if (msg == 'S') yNew = y - stepSize; if (msg == 'N') yNew = y + stepSize;
xNew = constrain(xNew, xMin, xMax); yNew = constrain(yNew, yMin, yMax); }
MoveServo(xServo, x, xNew); MoveServo(yServo, y, yNew); x = xNew; y = yNew; }
void MoveServo(Servo servo, int moveFrom, int moveTo) { if (moveFrom <= moveTo) { for (int c=moveFrom; c<=moveTo; c++) { servo.write(c); delay(50); } }
if (moveFrom > moveTo) { for (int c=moveFrom; c>=moveTo; c--) { servo.write(c); delay(50);
char motors = Serial.read(); switch(motors) { case 'R': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); break; case 'L': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); break; case 'F': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); break; case 'B': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); break; case 'O': digitalWrite(mA1,LOW); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,LOW); break; case 'X': digitalWrite(2,HIGH); break; case 'Z': digitalWrite(2, LOW); break; case 'H': digitalWrite(13, HIGH); delay(50); digitalWrite(13, LOW); } } } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #21 on: July 10, 2012, 07:11:01 am » |
i added servo pan and tilt code . when i compile theres no error but nothing is working . How can you tell? Serial data can flow both ways. Use some Serial.print() and/or Serial.println() statements to see where the program gets to. Where it doesn't get to is your problem. How are the pan and tilt servos powered? Not by the Arduino, I hope.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #22 on: July 10, 2012, 07:25:04 am » |
Use some Serial.print() and/or Serial.println() statements to see where the program gets to. i dont know how . can u help me How are the pan and tilt servos powered? Not by the Arduino, I hope. not by arduino
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #23 on: July 10, 2012, 07:28:40 am » |
i dont know how . can u help me This is trivially simple. Wherever you want to know that the code got to, add: Serial.println("I got to point such and such..."); To see what value a variable has, use something like: Serial.print("XXX = "); Serial.println(XXX); not by arduino OK. Now I just know that they are powered correctly. Thanks for clearing that up. Not.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #24 on: July 12, 2012, 04:20:57 pm » |
can u help me combin this two codes . tried many times still no luck. #include <Servo.h>
Servo xServo; Servo yServo;
int stepSize = 10;
const int xInit = 90; const int yInit = 90; const int xMin = 0; const int xMax = 180; const int yMin = 0; const int yMax = 180;
int x = xInit; int y = yInit; int xNew = x; int yNew = y;
void setup() { Serial.begin(9600); //establishContact(); // send a byte to establish contact until receiver responds
xServo.attach(5); // attaches the servo on pin 9 to the servo object yServo.attach(6); // attaches the servo on pin 9 to the servo object
delay(1000); MoveServo(xServo, xServo.read(), x); MoveServo(yServo, yServo.read(), y); delay(100); pinMode(11, OUTPUT); }
void Beep() { digitalWrite(11,HIGH); delay(250); digitalWrite(11,LOW); }
void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { Beep(); char msg = Serial.read();
if (msg == 'C') {xNew=xInit; yNew=yInit;}
if (msg == 'R') xNew = x - stepSize; if (msg == 'L') xNew = x + stepSize;
if (msg == 'D') yNew = y - stepSize; if (msg == 'U') yNew = y + stepSize;
xNew = constrain(xNew, xMin, xMax); yNew = constrain(yNew, yMin, yMax); }
MoveServo(xServo, x, xNew); MoveServo(yServo, y, yNew); x = xNew; y = yNew; }
void MoveServo(Servo servo, int moveFrom, int moveTo) { if (moveFrom <= moveTo) { for (int c=moveFrom; c<=moveTo; c++) { servo.write(c); delay(50); } }
if (moveFrom > moveTo) { for (int c=moveFrom; c>=moveTo; c--) { servo.write(c); delay(50); } } }
and int mA1 = 8; int mA2 = 9; int mB1 = 10; int mB2 = 11;
void setup(){ Serial.begin(9600);
pinMode(mA1, OUTPUT); pinMode(mA2, OUTPUT); pinMode(mB1, OUTPUT); pinMode(mB2, OUTPUT); pinMode(2, OUTPUT); pinMode(7, OUTPUT); }
void loop(){ if (Serial.available() > 0) { char motors = Serial.read(); switch(motors) { case 'R': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); delay(250); digitalWrite(mA1,LOW); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,LOW); break; case 'L': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); delay(250); digitalWrite(mA1,LOW); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,LOW); break; case 'F': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); break; case 'B': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); break; case 'O': digitalWrite(mA1,LOW); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,LOW); break; case 'X': digitalWrite(2,HIGH); break; case 'Z': digitalWrite(2, LOW); break; case 'H': digitalWrite(7, HIGH); delay(150); digitalWrite(7, LOW);
} } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #25 on: July 13, 2012, 12:17:01 am » |
can u help me combin this two codes First thing you need to do is explain what the two sketches do. Second thing you need to do is explain what the combined sketch is supposed to do. Third thing you need to do is post what you came up with, and explain what is wrong with it. Then, we can help you.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #26 on: July 13, 2012, 05:58:05 am » |
First thing you need to do is explain what the two sketches do. first sketch- controls two servo by letters U,D,R,L,C second sketch- controls two MOTOR by letters F,B,R,L,O Second thing you need to do is explain what the combined sketch is supposed to do. both the codes together Third thing you need to do is post what you came up with, and explain what is wrong with it. #include <Servo.h>
Servo xServo; Servo yServo;
int stepSize = 10;
const int xInit = 90; const int yInit = 90; const int xMin = 0; const int xMax = 180; const int yMin = 0; const int yMax = 180;
int x = xInit; int y = yInit; int xNew = x; int yNew = y;
int mA1 = 8; int mA2 = 9; int mB1 = 10; int mB2 = 11;
void setup(){ Serial.begin(9600);
pinMode(mA1, OUTPUT); pinMode(mA2, OUTPUT); pinMode(mB1, OUTPUT); pinMode(mB2, OUTPUT); pinMode(2, OUTPUT); pinMode(13, OUTPUT);
xServo.attach(5); // attaches the servo on pin 5 yServo.attach(6); // attaches the servo on pin 6
delay(1000); MoveServo(xServo, xServo.read(), x); MoveServo(yServo, yServo.read(), y); delay(100);
}
void loop(){ if (Serial.available() > 0) {
char msg = Serial.read();
if (msg == 'C') { xNew=xInit; yNew=yInit; }
if (msg == 'E') xNew = x - stepSize; if (msg == 'W') xNew = x + stepSize;
if (msg == 'S') yNew = y - stepSize; if (msg == 'N') yNew = y + stepSize;
xNew = constrain(xNew, xMin, xMax); yNew = constrain(yNew, yMin, yMax); }
MoveServo(xServo, x, xNew); MoveServo(yServo, y, yNew); x = xNew; y = yNew; }
void MoveServo(Servo servo, int moveFrom, int moveTo) { if (moveFrom <= moveTo) { for (int c=moveFrom; c<=moveTo; c++) { servo.write(c); delay(50); } }
if (moveFrom > moveTo) { for (int c=moveFrom; c>=moveTo; c--) { servo.write(c); delay(50);
char motors = Serial.read(); switch(motors) { case 'R': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); break; case 'L': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); break; case 'F': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); break; case 'B': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); break; case 'O': digitalWrite(mA1,LOW); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,LOW); break; case 'X': digitalWrite(2,HIGH); break; case 'Z': digitalWrite(2, LOW); break; case 'H': digitalWrite(13, HIGH); delay(50); digitalWrite(13, LOW); } } } }
it complies correctly but doesn't work
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #27 on: July 13, 2012, 06:02:30 am » |
first sketch- controls two servo by letters U,D,R,L,C second sketch- controls two MOTOR by letters F,B,R,L,O Clarification, please. but doesn't work Grrrr.
|
|
|
|
« Last Edit: July 13, 2012, 06:04:05 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 39
Arduino rocks
|
 |
« Reply #28 on: July 13, 2012, 07:21:38 am » |
sorry , in the code i have given N,S,W,E,C for the servos and for moving motors F,B,R,L,O to avoid character duplication #include <Servo.h>
Servo xServo; Servo yServo;
int stepSize = 10;
const int xInit = 90; const int yInit = 90; const int xMin = 0; const int xMax = 180; const int yMin = 0; const int yMax = 180;
int x = xInit; int y = yInit; int xNew = x; int yNew = y;
int mA1 = 8; int mA2 = 9; int mB1 = 10; int mB2 = 11;
void setup(){ Serial.begin(9600);
pinMode(mA1, OUTPUT); pinMode(mA2, OUTPUT); pinMode(mB1, OUTPUT); pinMode(mB2, OUTPUT); pinMode(2, OUTPUT); pinMode(13, OUTPUT);
xServo.attach(5); // attaches the servo on pin 5 yServo.attach(6); // attaches the servo on pin 6
delay(1000); MoveServo(xServo, xServo.read(), x); MoveServo(yServo, yServo.read(), y); delay(100);
}
void loop(){ if (Serial.available() > 0) {
char msg = Serial.read();
if (msg == 'C') { xNew=xInit; yNew=yInit; }
if (msg == 'E') xNew = x - stepSize; if (msg == 'W') xNew = x + stepSize;
if (msg == 'S') yNew = y - stepSize; if (msg == 'N') yNew = y + stepSize;
xNew = constrain(xNew, xMin, xMax); yNew = constrain(yNew, yMin, yMax); }
MoveServo(xServo, x, xNew); MoveServo(yServo, y, yNew); x = xNew; y = yNew; }
void MoveServo(Servo servo, int moveFrom, int moveTo) { if (moveFrom <= moveTo) { for (int c=moveFrom; c<=moveTo; c++) { servo.write(c); delay(50); } }
if (moveFrom > moveTo) { for (int c=moveFrom; c>=moveTo; c--) { servo.write(c); delay(50);
char motors = Serial.read(); switch(motors) { case 'R': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); break; case 'L': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); break; case 'F': digitalWrite(mA1,LOW); digitalWrite(mA2,HIGH); digitalWrite(mB1,HIGH); digitalWrite(mB2,LOW); break; case 'B': digitalWrite(mA1,HIGH); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,HIGH); break; case 'O': digitalWrite(mA1,LOW); digitalWrite(mA2,LOW); digitalWrite(mB1,LOW); digitalWrite(mB2,LOW); break; case 'X': digitalWrite(2,HIGH); break; case 'Z': digitalWrite(2, LOW); break; case 'H': digitalWrite(13, HIGH); delay(50); digitalWrite(13, LOW); } } } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #29 on: July 13, 2012, 12:16:34 pm » |
Second thing you need to do is explain what the combined sketch is supposed to do. both the codes together What does this mean? You must be very explicit when defining requirements. If you mean that the resulting sketch needs to respond to the letters N,S,W,E,C,F,B,R,L, and O, and perform code 1's functions for N,S,W,E,C and code 2's functions F,B,R,L,O, then say so, loop() and MoveServo() should NOT both be reading from serial. Only one function should. If both do, then a motor command is going to confuse the servo function.
|
|
|
|
|
Logged
|
|
|
|
|
|