Pages: [1]   Go Down
Author Topic: Controll two DC motors over wifi with Wifly  (Read 684 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

I'm currently working on a project that involves controlling the direction of two motors mounted on a chasis over wifi. It's like a wireless "tank"  but I'm having problems when I try to send data to the wifly shield. The Wifly shield (RN-131C) is connected to a Arduino UNO R3 that has a Motor shield R3 to control the two motors mentioned before. The sketch I designed works perfectly with the Arduino conected to the computer with an USB cable, and it uses the letters "W, A, S, D" of the keyboard to move in a specific direction. I use the hyperterminal and it works fine.
Now, my goal is, to make everything wirless and to be able to send the command (one of the letters) over the wifi and the vehicle should go to that direction, but I don't have a clue where to start. For now I managed to autoconnect the Wifly to my wifi network, but I can't connect to it because I don't know the IP adress, I think there's a command to set the IP to whatever I want, but I don't know it. Also I have searched for something similar to read the serial available for the Spi but I haven't found anything.

Here's the code That works without wifi connection, only through USB cable:

Code:

   void setup()
 {
  int a;
  for (a=5; a<=8; a++)
  pinMode (a, OUTPUT);
  Serial.begin(9600);
 
  pinMode(12, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT); 
   
 }
   
  void loop()
  {
   while (Serial.available() < 1) {} // Wait until a character is received
   char val = Serial.read();
   int leftspeed = 150; //255 is maximum speed
   int rightspeed = 150;
   switch(val) // Perform an action depending on the command
 {
   case 'w'://Move Forward
   forward (leftspeed,rightspeed);
   break;
   case 's'://Move Backwards
   reverse (leftspeed,rightspeed);
   break;
   case 'a'://Turn Left
   left (leftspeed,rightspeed);
   break;
   case 'd'://Turn Right
   right (leftspeed,rightspeed);
   break;
   default:
   digitalWrite(9,HIGH);
   digitalWrite(8,HIGH);
   break;
   }
    }
   
   void forward(char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 100);
   digitalWrite(13, HIGH);
   analogWrite(11, 100);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   
   }
   void reverse (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 100);
   digitalWrite(13, LOW);
   analogWrite(11, 100);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void left (char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 100);
   digitalWrite(13, LOW);
   analogWrite(11, 100);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void right (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 100);
   digitalWrite(13, HIGH);
   analogWrite(11, 100);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For those interested, I made it! After a long day of hardworking I am now capable of controlling my tank through the wifly, although the directions are somehow fucked up, I should be able to find the problem. If anyone knows let me know!
Here's the code, obiously it has the credentials changed to fit my network:

Code:
#include "WiFly.h"
#include "Credentials.h"

void setup() {

  int a;
  for (a=5; a<=8; a++)
  pinMode (a, OUTPUT);
  Serial.begin(9600);
 
  pinMode(12, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT);
 
  digitalWrite (9, HIGH);
  digitalWrite (8, HIGH);
 
  Serial.begin(9600);
  WiFly.begin();
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }
 
  Serial.println("Associated!");
     
   
 }


void loop() {
 
   while (SpiSerial.available() < 1) {} // Wait until a character is received
   char val = SpiSerial.read();
   int leftspeed = 150; //255 is maximum speed
   int rightspeed = 150;
   switch(val) // Perform an action depending on the command
 {
   case 'w'://Move Forward
   forward (leftspeed,rightspeed);
   break;
   case 's'://Move Backwards
   reverse (leftspeed,rightspeed);
   break;
   case 'a'://Turn Left
   left (leftspeed,rightspeed);
   break;
   case 'd'://Turn Right
   right (leftspeed,rightspeed);
   break;
   default:
   digitalWrite(9,HIGH);
   digitalWrite(8,HIGH);
   break;
   }
    }
   
   void forward(char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 150);
   digitalWrite(13, HIGH);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   
   }
   void reverse (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 150);
   digitalWrite(13, LOW);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void left (char a,char b)
   {
   digitalWrite(12, LOW);
   analogWrite(3, 150);
   digitalWrite(13, LOW);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }
   void right (char a,char b)
   {
   digitalWrite(12, HIGH);
   analogWrite(3, 150);
   digitalWrite(13, HIGH);
   analogWrite(11, 150);
   
   digitalWrite(9,LOW);
   digitalWrite(8,LOW);
   }

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, i know what's happening, it seems that the motor that's conencted to the pin 13 (direction) and pin 8 (brake) are also used by the wifly, so my question now is, how do I change the pins so they don't have any conflict?
Logged

Southern Ontario
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Jaks13,

looking at the wifly schematic you should be able to re-assign the motor pins in your sketch to anything from D2 - D6 and it should work.

Hope this helps ...and thanks for reporting your progress !
Build_it_Bob
Logged

Pages: [1]   Go Up
Jump to: