Arduino based robot

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
warriors1980
 
Posts: 3
Joined: Fri Jul 22, 2016 6:26 pm

Arduino based robot

Post by warriors1980 »

I am trying to create an Arduino based robot. It has an Adafruit motor controller shield, RFM69HCW radio module, motors from servocity, and 3D printed parts. The controller consists of a Adafruit Feather 32u4 RFM69HCW Packet Radio and 2 buttons right now. Pressing a button on the controller will make the wheels turn, but they only turn for a set amount of time regardless of how long I hold the button. Also, I tried using print statements in the serial monitor to debug what is going on, but the serial monitor phrases after pressing a button.

Can someone tell me what I am doing wrong?

Code: Select all

//Robot Code
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <RFM69.h>    //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h>

#define NETWORKID     100  //the same on all nodes that talk to each other
#define NODEID        1  

#define FREQUENCY      RF69_915MHZ
#define ENCRYPTKEY     "codeOn1234567890" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HCW    true // set to 'true' if you are using an RFM69HCW module
#define SERIAL_BAUD   9600
#define RFM69_CS      10
#define RFM69_IRQ     2
#define RFM69_IRQN    0  // Pin 2 is IRQ 0!
#define RFM69_RST     9 

RFM69 radio = RFM69(RFM69_CS, RFM69_IRQ, IS_RFM69HCW, RFM69_IRQN);
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motorLeft = AFMS.getMotor(3);
Adafruit_DCMotor *motorRight = AFMS.getMotor(4);

int16_t packetnum = 0;  // packet counter, we increment per xmission
char flag = 's';
Adafruit_DCMotor *configureMotor(Adafruit_DCMotor *motor, int power = 0, String direction = "stop"){
  if (direction == "forward"){
    motor->run(FORWARD);
    motor->setSpeed(power);
  }
  else if (direction == "backward"){
    motor->run(BACKWARD);
    motor->setSpeed(power);
  }
  else if (direction == "stop"){
    motor->setSpeed(0);
  }
  else Serial.println("bad command");
  return motor;
  }

void moveForward(int power, int duration){
  motorLeft = configureMotor(motorLeft, power, "forward");
  motorRight = configureMotor(motorRight, power, "backward");
}

void moveBack(int power, int duration){
  Serial.print("b");
  motorLeft = configureMotor(motorLeft, power, "backward");
  motorRight = configureMotor(motorRight, power, "forward");
}

void moveStop(int duration){
  motorLeft = configureMotor(motorLeft, "stop");
  motorRight = configureMotor(motorRight, "stop");
}

void setup() {
  Serial.begin(SERIAL_BAUD);  
  AFMS.begin();
  // Hard Reset the RFM module
  pinMode(RFM69_RST, OUTPUT);
  digitalWrite(RFM69_RST, HIGH);
  delay(100);
  digitalWrite(RFM69_RST, LOW);
  delay(100);
  
  // Initialize radio
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.setPowerLevel(31); // power output ranges from 0 (5dBm) to 31 (20dBm)
  radio.encrypt(ENCRYPTKEY);

  //Turn motors off
  motorRight = configureMotor(motorRight, "stop");
  motorLeft = configureMotor(motorLeft, "stop");
}
 
void loop() {
  Serial.print((char*)radio.DATA);
  radio.receiveDone(); //put radio in RX mode
  Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU
  Serial.print("\n"); 
  if ((char)radio.DATA[1] == '0' && flag == 'g'){
    Serial.print(flag);
    Serial.print("button 1");
    moveForward(200, 10000);
    }
  else if ((char)radio.DATA[2] == '0'){
    Serial.print("button 2");
    moveBack(100, 100);
    }
  else moveStop(1);
}

Code: Select all

//Controller code
#include <RFM69.h>
#include <SPI.h>

#define NETWORKID     100  // The same on all nodes that talk to each other
#define NODEID        2    // The unique identifier of this node
#define RECEIVER      1    // The recipient of packets

#define FREQUENCY     RF69_915MHZ
#define ENCRYPTKEY    "codeOn1234567890" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HCW   true // set to 'true' if you are using an RFM69HCW module

//*********************************************************************************************
#define SERIAL_BAUD   9600

#define RFM69_CS      8
#define RFM69_IRQ     7
#define RFM69_IRQN    4  // Pin 7 is IRQ 4!
#define RFM69_RST     4

#define LED           13  // onboard blinky

int button = A0;
int16_t packetnum = 0;  // packet counter, we increment per xmission

/***Testing***/
int forward = A5;
int back = A4;
RFM69 radio = RFM69(RFM69_CS, RFM69_IRQ, IS_RFM69HCW, RFM69_IRQN);

void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(forward, INPUT_PULLUP);
  pinMode(back, INPUT_PULLUP);
  Serial.begin(SERIAL_BAUD);
  pinMode(RFM69_RST, OUTPUT);
  digitalWrite(RFM69_RST, HIGH);
  delay(100);
  digitalWrite(RFM69_RST, LOW);
  delay(100);

  // Initialize radio
  radio.initialize(FREQUENCY, NODEID, NETWORKID);
  if (IS_RFM69HCW) {
    radio.setHighPower();    // Only for RFM69HCW & HW!
  }
  radio.setPowerLevel(31); // power output ranges from 0 (5dBm) to 31 (20dBm)
  radio.encrypt(ENCRYPTKEY);
  pinMode(LED, OUTPUT);
}


void loop() {
  delay(500);  // Wait 1 second between transmits, could also 'sleep' here!
  char radiopacket[25] = "[000 000 0 0 0 0 0 0]";  

  if (digitalRead(forward) == HIGH) {radiopacket[1] =  '1';}
  if (digitalRead(back) == HIGH) {radiopacket[2] =  '1';}
  
  radio.receiveDone(); //put radio in RX mode
  Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU
}
Last edited by adafruit_support_bill on Tue Jul 26, 2016 2:24 pm, edited 2 times in total.
Reason: Please use [code] tags when submitting code to the forums

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino based robot

Post by adafruit_support_bill »

It is possible that the motor brush noise is interfering with your radio transmissions. Try adding some capacitors to the motor as described in the motor shield FAQ.

User avatar
warriors1980
 
Posts: 3
Joined: Fri Jul 22, 2016 6:26 pm

Re: Arduino based robot

Post by warriors1980 »

Thanks. I did not have any capacitors so after reading your post, I decided to solder on the wire antennas. It worked with out them before, so I just kept going. Now the Arduino is not receiving messages from the feather. I am going to buy some capacitors now. Did I solder the wires on wrong or something?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino based robot

Post by adafruit_support_bill »

If you post some photos of your soldering, we'll take a look.

If you get the radios working again, another way to test for motor interference is to disconnect the motors and look at the serial output. If you can send commands reliable with no motors actually running, then the motors are likely the problem.

User avatar
warriors1980
 
Posts: 3
Joined: Fri Jul 22, 2016 6:26 pm

Re: Arduino based robot

Post by warriors1980 »

I tried uploaded pictures to google photos to post, but they did not post correctly did not work.

Image
Image
the links are
https://goo.gl/photos/qgnsNmgyuCWqCb7r8
https://goo.gl/photos/WZyCgshArSVE8Uwy6

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino based robot

Post by adafruit_support_bill »

The first one looks a little rough. Make sure there are no solder-bridges from the wire to either of the two side pads.

The second one looks good and clean. Both should probably be trimmed a little closer to the board to avoid any accidental shorts.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”