Pages: 1 [2] 3   Go Down
Author Topic: code modification help  (Read 1947 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
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 Offline
Brattain Member
*****
Karma: 302
Posts: 26291
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

so should i do this

Code:

case 'H':
        digitalWrite(13, HIGH);
        delay(1000);
        digitalWrite(13, LOW);
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26291
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

thanks

i got both work now.

next planing to add pan & tilt
Logged

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

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.  smiley-eek

Code:
#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 Offline
Brattain Member
*****
Karma: 613
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Use some Serial.print() and/or Serial.println() statements to see where the program gets to.

i dont know how . can u help me

Quote
How are the pan and tilt servos powered? Not by the Arduino, I hope.

not by arduino
Logged

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

Quote
i dont know how . can u help me
This is trivially simple. Wherever you want to know that the code got to, add:
Code:
Serial.println("I got to point such and such...");

To see what value a variable has, use something like:
Code:
Serial.print("XXX = ");
Serial.println(XXX);

Quote
not by arduino
OK. Now I just know that they are powered correctly. Thanks for clearing that up. Not.
Logged

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

can u help me combin this two codes . tried many times still no luck.

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

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

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

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 39
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Quote
Second thing you need to do is explain what the combined sketch is supposed to do.
both the codes together
Quote
Third thing you need to do is post what you came up with, and explain what is wrong with it.
Code:
#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 Offline
Brattain Member
*****
Karma: 302
Posts: 26291
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

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

Code:
#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 Offline
Brattain Member
*****
Karma: 613
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Second thing you need to do is explain what the combined sketch is supposed to do.
Quote
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

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