Go Down

Topic: code modification help (Read 2 times) previous topic - next topic

crxksa

Quote
But you didn't turn the pin off.


how do i do tat

sorry for my lack of knowledge in coding

AWOL

Like most digital operations, it is the opposite of turning it on, which you did just before the delay
"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.

crxksa

so should i do this

Code: [Select]

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

AWOL

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

crxksa

thanks

i got both work now.

next planing to add pan & tilt

crxksa

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: [Select]
#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);
      } 
    }
  }
}




PaulS

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.

crxksa

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

PaulS

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: [Select]
Serial.println("I got to point such and such...");

To see what value a variable has, use something like:
Code: [Select]
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.

crxksa

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

Code: [Select]
#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: [Select]
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);

   }  
 }
}


PaulS

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.

crxksa

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: [Select]
#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


AWOL

#27
Jul 13, 2012, 01:02 pm Last Edit: Jul 13, 2012, 01:04 pm by AWOL Reason: 1
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.
"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.

crxksa

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: [Select]
#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);
      } 
    }
  }
}



PaulS

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.

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview