Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: code modification help on: July 24, 2012, 02:47:38 pm
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);

    } 
  }
}

2  Using Arduino / General Electronics / Re: laptop's battery info to arduino on: July 23, 2012, 08:52:32 am
what i meant is showing the battery info from windows 7 to arduino through serial communication (to show  the battery percentage shown near the clock. bottom-right  )  . not to connect the arduino to laptop battery
3  Using Arduino / General Electronics / laptop's battery info to arduino on: July 22, 2012, 07:56:14 pm
I was wondering if there is a way to send the amount of charge left in a laptop's battery through usb to an arduino and display it on an LCD 16x2 , maybe some kind of script running in the background. Any ideas? I would appreciate the help
4  Using Arduino / Programming Questions / Re: code modification help on: July 13, 2012, 01:21:10 pm
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
5  Using Arduino / Programming Questions / Re: code modification help 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

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);
      } 
    }
  }
}

6  Using Arduino / Programming Questions / Re: code modification help on: July 13, 2012, 05:58:05 am
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

7  Using Arduino / Programming Questions / Re: code modification help on: July 12, 2012, 04:20:57 pm
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);

    }  
  }
}

8  Using Arduino / Programming Questions / Re: code modification help on: July 10, 2012, 07:25:04 am
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
9  Using Arduino / Programming Questions / Re: code modification help 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.  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);
      } 
    }
  }
}



10  Using Arduino / Programming Questions / Re: code modification help on: July 09, 2012, 07:06:36 am
thanks

i got both work now.

next planing to add pan & tilt
11  Using Arduino / Programming Questions / Re: code modification help on: July 09, 2012, 06:51:07 am
so should i do this

Code:

case 'H':
        digitalWrite(13, HIGH);
        delay(1000);
        digitalWrite(13, LOW);
12  Using Arduino / Programming Questions / Re: code modification help on: July 09, 2012, 06:39:35 am
Quote
But you didn't turn the pin off.

how do i do tat

sorry for my lack of knowledge in coding
13  Using Arduino / Programming Questions / Re: code modification help on: July 09, 2012, 06:07:28 am
but i gave a delay 1000

it for a buzzer
14  Using Arduino / Programming Questions / Re: code modification help on: July 09, 2012, 05:49:20 am
when i sending "X and Y" the led works . on and off
but when i sending "H" even though i gave a delay of 1000 milliseconds it stays on .

i tried with and without break

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(13, 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);
      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 'S':
      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(1000);

    } 
  }
}

15  Using Arduino / Programming Questions / Re: code modification help on: July 09, 2012, 01:06:44 am
I will give it test asap . And tell u the result
Pages: [1] 2 3