Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Luis Estades
Published

ZomoBot Controller - Arcade Joystick Contribution

My Personal Contribution to the building of this project

AdvancedWork in progress63
ZomoBot Controller - Arcade Joystick Contribution

Things used in this project

Hardware components

joystick walfront
×1
Joystick Encoder board
×1
Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Story

Read more

Code

Reciever Code

Arduino
//  Modified version of ask_receiver.pde by RadioHead
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
//Enable timmer2 instead of timmer1
#define RH_ASK_ARDUINO_USE_TIMER2
RH_ASK driver(2000, 11, 12, 13, false);;
#include <ZumoMotors.h>
ZumoMotors motors;
float Sensor1Data;
char Sensor1CharMsg[4];
float Sensor2FloatMsg[3];
int left_speed, right_speed;
void setup()
{
  //LED indicates message sent.
  pinMode(13, OUTPUT);
  Serial.begin(9600); // Debugging only.
  //For some reason, this is needed to initialize the driver.
  if (!driver.init()) {
    Serial.println("init failed");
  }
}
void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buf2[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  uint8_t buflen2 = sizeof(buf);
  float speedMult = 0.5;
  if(driver.recv(buf2, &buflen2))
  {
    if()
  }
  
  if (driver.recv(buf, &buflen)) // Non-blocking
  {
    int i;
    // Message with a good checksum received, dump it.
    for (i = 0; i < buflen; i++)
    {
      // Fill Sensor1CharMsg Char array with corresponding
      // chars from buffer.
      Sensor1CharMsg[i] = char(buf[i]);
      Sensor2FloatMsg[i]= float(buf[i]);
    }
    // Null terminate the char array
    // This needs to be done otherwise problems will occur
    // when the incoming messages has less digits than the
    // one before.
    Sensor1CharMsg[buflen] = '\0';
    // Convert Sensor1CharMsg Char array to integer
    Sensor1Data = atof(Sensor1CharMsg);    
 
    //Choose the correct speeds depending on the command
    if (Sensor1CharMsg[0] == 'f') {
      left_speed = 200;
      right_speed = 200;
    }
    if (Sensor1CharMsg[0] == 'b') {
      left_speed = -200;
      right_speed = -200;
    }
    if (Sensor1CharMsg[0] == 'n') {
      left_speed = 0;
      right_speed = 0;
    }
    if (Sensor1CharMsg[1] == 'l') {
      left_speed = left_speed + 100;
      right_speed = right_speed - 100;
    }
    if (Sensor1CharMsg[1] == 'r') {
      left_speed = left_speed - 100;
      right_speed = right_speed + 100;
    }
    if (Sensor1CharMsg[1] == 'n') {
      left_speed = left_speed;
      right_speed = right_speed;
    }
 
    //Activate motors
    ZumoMotors::setSpeeds(left_speed, right_speed);
 
    //Flash to indicate message received.
    digitalWrite(13, 1);
    delay(200);
    digitalWrite(13, 0);
    delay(200);
 
  }
}

Transmitter Code

Arduino
// Modified version of ask_transmitter.pde by RadioHead
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
const int potPin = A0;
int value;
void setup()
{
  //LED indicates message sent.
  //pinMode(13, OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
  //For some reason, this is needed to initialize the driver.
  if (!driver.init())
  {
    Serial.println("init failed");
  }
}
 
void loop()
{
  int switchStateFwd = digitalRead(4);
  int switchStateBwd = digitalRead(8);
  int switchStateRgt = digitalRead(5);
  int switchStateLft = digitalRead(9);
  //int speedMagnitude = analogRead(A0);
  float voltage1 = analogRead(A0) * (5.0 / 1023.0);
  //value = analogRead(potPin);
  //value = map(value, 0, 1023, 0, 255);
  delay(1); 
  Serial.println(voltage1);
  char msg[8];
  float value
  //Read the voltage in the potentiometer.
  //This one encodes forward/neutral/backwards
  //float voltage1 = analogRead(0) * (5.0 / 1023.0);
  
  //Read the voltage in the potentiometer.
  //This one encodes left/neutral/rigth
  //float voltage2 = analogRead(1) * (5.0 / 1023.0);
  if(voltage1 > .5)
  {
    msg[0] = voltage1;
  }
  else
  {
    msg[0] = .5
  }
  //move foward
  if(switchStateFwd == LOW)
  {
    msg[0] = 'n';
    //Serial.println("Stop!");
  }
  else
  {
    msg[0] = 'f';
    //Serial.println("Forward!");
  }
  
  //move back
  if(switchStateBwd == LOW)
  {
    //msg[0] = 'n';
  }
  else
  {
    msg[0] = 'b';
    //Serial.println("Back!");
  }
  
  //move left
  if(switchStateRgt == LOW)
  {
    msg[1] = 'n';
  }
  else
  {
    msg[1] = 'r';
    //Serial.println("Left!");
  }
  
  //move right
  if(switchStateLft == LOW)
  {
    //msg[1] = 'n';
  }
  else
  {
    msg[1] = 'l';
    //Serial.println("Right!");
  }
  

  /*if (voltage1 > 2.6) {
    msg[0] = 'f';
  } else if (voltage1 < 2.4) { msg[0] = 'b'; } else { msg[0] = 'n'; } if (voltage2 > 2.6) {
 
    msg[1] = 'r';
  } else if (voltage2 < 2.4) {
    msg[1] = 'l';
  }
  else {
    msg[1] = 'n';
  }*/
 
  //Send the message and wait to check if it was sent.
  driver.send((uint8_t *)msg, strlen(msg));
  driver.waitPacketSent();
 
  //Flash LED to indicate message sent.
  digitalWrite(13, 1);
  delay(20);
  digitalWrite(13, 0);
  delay(20);
}

Credits

Luis Estades

Luis Estades

13 projects • 1 follower

Comments