Pages: 1 [2] 3   Go Down
Author Topic: Varible Conversion & Serial question  (Read 1551 times)
0 Members and 1 Guest are viewing this topic.
Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh okey, i understand now. and you are right about my basics of c, they are bad, but im working on it ^^.
anyway my brother does program c++ and tryed to explain me some parts, he sucseed but that doesnt take all problems away..

i've copied and edit my code now, i have this:
Code:
#include <VirtualWire.h>
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
  //Setup VirtualWire to transmit on 4
  vw_setup(100);
  vw_set_tx_pin(4);
  
  pinMode(13, OUTPUT);    //Power LED
  digitalWrite(13, HIGH); //Always ON
  
  //setup a serial connection
  Serial.begin(57600);
}

void loop()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
  
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    vw_send((uint8_t *)inData, strlen(inData));
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

here is the code of the robot wich im trying to control:
Code:
#include <VirtualWire.h>

int m1r = 6;  //motor left red wire
int m1b = 5;  //motor left black wire
int m2r = 9;  //motor right red wire
int m2b = 10; //motor right black wire

void setup()
{
  //virtual wire setup
  //for recive on pin 4
  vw_setup(100);
  vw_rx_start();
  vw_set_rx_pin(4);
  
  pinMode(13, OUTPUT);    //power LED
  digitalWrite(13, HIGH); //Always ON
}

void loop()
{
  //variables setup
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  char recived;
  
  if(vw_get_message((uint8_t*)recived, &buflen))
  {
    move(recived);
  }
  
  delay(50);
}


//[FUNCTION] ~ move into recived driection (for,bac,lef,rig)
void move(char DIR)
{
  //Stop
  if(DIR == 'x')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 0);
    analogWrite(m2r, 0);
    analogWrite(m2b, 0);
  }
  //drive forward
  if(DIR == 'f')
  {
    analogWrite(m1r, 255);
    analogWrite(m1b, 0);
    analogWrite(m2r, 255);
    analogWrite(m2b, 0);
  }
  //drive backwards
  if(DIR == 'b')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 255);
    analogWrite(m2r, 0);
    analogWrite(m2b, 255);
  }
  //turn left
  if(DIR == 'l')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 255);
    analogWrite(m2r, 255);
    analogWrite(m2b, 0);
  }
  //turn right
  if(DIR == 'r')
  {
    analogWrite(m1r, 255);
    analogWrite(m1b, 0);
    analogWrite(m2r, 0);
    analogWrite(m2b, 255);
  }
}

now if i power both and send the letter 'r' trough the serial monitor it doesn't turn right,
is this because of wrong transmition or because something else? (i hope im not going offtoppic)

thanks alot already,
nick
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

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

The packet (r) needs to be between start and end markers. Try sending "<r>", instead of "r".
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i forgot, and tryed it by sending: <f>
but it didn't change anything still no movement..
any idea's?
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This doesn't look good:
Code:
  if(vw_get_message((uint8_t*)recived, &buflen))

By the look of the foregoing, it should be
Code:
  if(vw_get_message(buf, &buflen))
and then you'll need to pass buf[1] (perhaps) to Move.
Logged

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

Quote
any idea's?
As a matter of fact, yes. Add some serial output. Are you receiving what you think you are?

The move() function on the receiver is passed an array of chars. It expects a char. Fail!

Some of the motor control wires control direction, and should be connected to non-PWM pins, with digitalWrite() used to control them. Do the motors do what they are supposed to with a simple sketch on the receiver?

What, exactly, is connecting the two Arduinos?
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okey i figured i had my transmit module wired incorrect (5v to ground, and ground to 5v) could this 'kill' my module?
i changed code many many times already and checked all connected pins (wich are now correct!), lost of testing,
i added a LED on d13 wich flips state on reciving data, but sadly the power led never goes LOW. (this is the UNO on breadbord)

Problem: or im not transmitting, or im not reciving..

note's:
also i added a serial.print function wich prints the inData after it 'should' be transmitted by rf.
i changed pins from rf modules to defauls for VirtualWire (from 4 to 11 for RX, & 12, TX)

here's the codes:

Control Unit
Code:
#include <VirtualWire.h>
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
  //setup a serial connection
  Serial.begin(57600);
  
  //Setup VirtualWire to transmit on 4
  vw_setup(100);
  
  pinMode(13, OUTPUT);    //Power LED
  digitalWrite(13, HIGH); //Always ON
}

void loop()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
  
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    vw_send((uint8_t *)inData, strlen(inData));
    Serial.println(inData);
    
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  
  delay(40);
}

Device
Code:
#include <VirtualWire.h>

int m1r = 6;  //motor left red wire
int m1b = 5;  //motor left black wire
int m2r = 9;  //motor right red wire
int m2b = 10; //motor right black wire

void setup()
{
  //virtual wire setup
  //for recive on pin 4
  vw_setup(100);
  vw_rx_start();
  
  pinMode(13, OUTPUT);    //power LED
  digitalWrite(13, HIGH); //Always ON
}

void loop()
{
  //variables setup
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  
  if(vw_get_message((uint8_t*)buf, &buflen))
  {
    move(char(buf[0]));
    digitalWrite(13, !digitalRead(13)); //flip POWER led
  }
}


//[FUNCTION] ~ move into recived driection (for,bac,lef,rig)
void move(char DIR)
{
  //Stop
  if(DIR == 'x')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 0);
    analogWrite(m2r, 0);
    analogWrite(m2b, 0);
  }
  //drive forward
  if(DIR == 'f')
  {
    analogWrite(m1r, 255);
    analogWrite(m1b, 0);
    analogWrite(m2r, 255);
    analogWrite(m2b, 0);
  }
  //drive backwards
  if(DIR == 'b')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 255);
    analogWrite(m2r, 0);
    analogWrite(m2b, 255);
  }
  //turn left
  if(DIR == 'l')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 255);
    analogWrite(m2r, 255);
    analogWrite(m2b, 0);
  }
  //turn right
  if(DIR == 'r')
  {
    analogWrite(m1r, 255);
    analogWrite(m1b, 0);
    analogWrite(m2r, 0);
    analogWrite(m2b, 255);
  }
}



@PaulS:
Code:
As a matter of fact, yes. Add some serial output. Are you receiving what you think you are?
okey i dit so, i am outputting the inData from the arduino board. also i added for the device/robot a little code to flip power led (from high to low and from low to high [D13])

Code:
What, exactly, is connecting the two Arduinos?
the 'two' arduino's are connected by air..
i got these arduino's:
1x uno
1x uno on a breadboard (http://arduino.cc/en/Main/Standalone)

the broadboard uno has the reciver and is the device with motors, next to him lies the arduino uno with serial connection (usb) to the pc communicating with arduino IDE

Code:
Some of the motor control wires control direction, and should be connected to non-PWM pins, with digitalWrite() used to control them. Do the motors do what they are supposed to with a simple sketch on the receiver?
i have tested my motors before without RF communication to controll them with pwm, and it sucseed (PS: im using a half h-bridge)
(if i am not mistaking) if d9 outputs 5v it is outputting a digital 1 or HIGH, wether it is by pwm(255) or by digitalwrite(HIGH)..
( im sorry if i missunderstand, my english isn't the best..)

Code:
The move() function on the receiver is passed an array of chars. It expects a char. Fail!
i geus i changed that now.. (look at new code)
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

did my post sink in all other posts without notice, or does nobody have a clue what could be wrong here?
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

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

Quote
the 'two' arduino's are connected by air..
Well, there's your problem. The Arduino's a no good at transmitting data by air.

Quote
i added a LED on d13 wich flips state on reciving data, but sadly the power led never goes LOW. (this is the UNO on breadbord)
The power LED on what? Why would you want the power LED to turn off?

Quote
did my post sink in all other posts without notice, or does nobody have a clue what could be wrong here?
You are not answering questions correctly, or are giving incomplete answers. It is hard to help you when you do that.
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You are not answering questions correctly, or are giving incomplete answers. It is hard to help you when you do that.
oh, okey ill try a little bit harder..

what i mean by connected with air, means that there are no wires wich connects them.
and about the power led.. it indicates that power is ON, but i want i to turn off when i recive data, this is the only way that i know that data is recived.
but since the powerled doesnt turn off, there is either no data recived or transmitted.

any other questions wich i answering incrorrect or incomplete?

thnx alot for all the efford,
nick
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

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

Quote
what i mean by connected with air, means that there are no wires wich connects them.
If there is nothing connecting them, how do you expect data to get from one to the other?

Quote
and about the power led.. it indicates that power is ON, but i want i to turn off when i recive data, this is the only way that i know that data is recived.
but since the powerled doesnt turn off, there is either no data recived or transmitted.
On my Arduino, the only way to turn the power LED off is to remove the power source. I suppose you could connect a relay, and disconnect the power to the Arduino, by toggling the relay when data is received, but that defeats the purpose of having received data, doesn't it?

Unless you are talking about some other LED, in which case it isn't the power LED.
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i connected a led to D13 on the arduino uno on a breadboard. next my code puts D13 high in the Setup(){}
so im not talking about the arduino uno wich transmits, but im talking about the arduino uno standalone wich should recive.
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

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

Quote
i connected a led to D13 on the arduino uno on a breadboard. next my code puts D13 high in the Setup(){}
so im not talking about the arduino uno wich transmits, but im talking about the arduino uno standalone wich should recive.
So, you are not talking about the power LED, and you still having nothing that sends data and nothing that receives data. And, you still can't understand why you can't turn the power LED off when data is received.

Did I miss something?
Logged

Holland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 93
Troubles are there to be solved!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dit i forgot to mension that i am using a tranmitterr module & and reciver module wich commonicate trough RF (radiofrequenty) at 434Mhz?
maybe you are overlooking page 1?
Logged

humankind cannot obtain anything without
sacrificing something of equal value.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You never explicitly mentioned it on page 1, you implied it, but subsequent posts led us to believe you thought RF was native to the MCU.

Have you gotten the virtualwire example code working?
Logged

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

Quote
You never explicitly mentioned it on page 1, you implied it
Exactly. Specifically, you have not mentioned WHICH RF modules you are using. That is what I have been trying to get you to do. Post links the modules, and show how they are connected to the Arduinos, and then we will have something to work with.
Logged

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