Pages: 1 2 [3]   Go Down
Author Topic: code modification help  (Read 1944 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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,
yes, iam making a rover with pan and tilt cam
Quote
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.
i don't understand ,plz can u show in the code i posted
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49256
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i don't understand ,plz can u show in the code i posted
Sure. In loop(), you check for serial data, and is there is any, you read the first byte:
Code:
void loop(){
  if (Serial.available() > 0) {


    char msg = Serial.read();

Then, you decide what, if anything, to do, based on that letter.

Then, you call MoveServo(), which again reads serial data:
Code:
void MoveServo(Servo servo, int moveFrom, int moveTo)
{
<snip>
  if (moveFrom > moveTo)
  {
    for (int c=moveFrom; c>=moveTo; c--)
    {
      servo.write(c);
      delay(50);


      char motors = Serial.read();
      switch(motors) {

If the letter F arrives, loop() doesn't have an action associated with that letter, so it does nothing. Then MoveServo() gets called, and possibly reads some serial data, even of there was none to read. Why the reading only happens in some situations, but not others, escapes me. Why the reading is done in a for loop that iterates a number of times based on the angle between where the servo is and where it should be is a mystery, too.

I can see no reason when MoveServo() should be reading the serial port at all.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

forget about combining two codes together I don't understand sorry . just help me add two servos to the code ,which can be moved by letter N,S,W,E,C .

Code:
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

Pages: 1 2 [3]   Go Up
Jump to: