Pages: [1]   Go Down
Author Topic: rc car  (Read 2472 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Who you gonna call?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am trying to hack an rc car and my arduino code is for some reasoen messed up if you can run it on arduino and tell me what is wrong than thanks.



Code:
//
// SuperCar_Echo_Test Sketch
// V1.0
//
// Test code for RoboTech Center SuperCar kit.
// Under Tools->Board select Arduino Nano w/ATmega 328
//
// You also need to install the NewPIng and Adafruit_NeoPixel libraries:
// http://playground.arduino.cc/Code/NewPing
// https://github.com/adafruit/Adafruit_NeoPixel
//
#include <NewPing.h>
#include <Adafruit_NeoPixel.h>
 
// Input signals from R/C receiver
#define FWD_RC_IN A6  // Analog input only
#define BACK_RC_IN A7  // Analog input only
#define RIGHT_RC_IN 4
#define LEFT_RC_IN 5
 
// Output signals to motor driver
#define FWD_MICRO_OUT 10
#define BACK_MICRO_OUT 6
#define RIGHT_MICRO_OUT 11
#define LEFT_MICRO_OUT 9
 
// Signal to select Microcontroller (low) or R/C (high) control
#define MICRO_L_RC_H 12
 
// Arduino Nano LED
#define NANO_LED 13
 
// IO Ports as labeled on Add-on board
#define IO_A1 2
#define IO_A2 8
#define IO_B1 A5
#define IO_B2 A4
#define IO_C1 A3
#define IO_C2 A2
#define IO_D 3
#define IO_E A1
#define IO_F A0
 
// Configure Sonar on I/O A
#define SonarEcho IO_A1
#define SonarTrigger IO_A2
#define SonarMaxDistance 30 // in cm
NewPing frontSonar(SonarTrigger, SonarEcho, SonarMaxDistance);
 
// LED Strip on I/O B1
#define LEDStrip IO_B1
Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(4, LEDStrip, NEO_GRB + NEO_KHZ800);
 
// Wheel Lights
#define WheelLights IO_D
 
// Internal variables
byte superCarStatus;
#define Mode 0x01<<0
#define MicroMode 0x00<<0
#define RcMode 0x01<<0
 
#define Drive 0x03<<1
#define Stopped 0x00<<1
#define Forward 0x01<<1
#define Backward 0x02<<1
 
#define Turn 0x03<<3
#define Straight 0x00<<3
#define Right 0x01<<3
#define Left 0x02<<3
 
// Sonar on I/O A
#define SonarEcho IO_A1
#define SonarTrigger IO_A2
 
// LED Strips on I/O B
#define FrontLEDStrip IO_B1
#define TopLED IO_B2
 
// Rear Wheel Lights
#define WheelLights IO_D
 
byte isDriving()
{
  return(superCarStatus & Drive);
}
 
byte isTurning()
{
  return(superCarStatus & Turn);
}
 
byte isMode()
{
  return(superCarStatus & Mode);
}
 
byte isRcDrive()
{
  byte ret;
  
  if (analogRead(FWD_RC_IN) > 128)
    ret = Forward;
  else if (analogRead(BACK_RC_IN) > 128)
    ret = Backward;
  else
    ret = Stopped;
 
  return(ret);
 
}
 
byte isRcTurn()
{
  byte ret;
  
  if (digitalRead(RIGHT_RC_IN) == HIGH)
    ret = Right;
  else if (digitalRead(LEFT_RC_IN) == HIGH)
    ret = Left;
  else
    ret = Straight;
    
  return(ret);
}
 
void stop()
{
  digitalWrite(FWD_MICRO_OUT, LOW);
  digitalWrite(BACK_MICRO_OUT, LOW);
  superCarStatus &= ~Drive;
  superCarStatus |= Stopped;
}
 
void forward()
{
  digitalWrite(BACK_MICRO_OUT, LOW);
  digitalWrite(FWD_MICRO_OUT, HIGH);
  superCarStatus &= ~Drive;
  superCarStatus |= Forward;
}
 
void backward()
{
  digitalWrite(FWD_MICRO_OUT, LOW);
  digitalWrite(BACK_MICRO_OUT, HIGH);
  superCarStatus &= ~Drive;
  superCarStatus |= Backward;
}
 
void straight()
{
  digitalWrite(RIGHT_MICRO_OUT, LOW);
  digitalWrite(LEFT_MICRO_OUT, LOW);
  superCarStatus &= ~Turn;
  superCarStatus |= Straight;
}
 
void right()
{
  digitalWrite(LEFT_MICRO_OUT, LOW);
  digitalWrite(RIGHT_MICRO_OUT, HIGH);
  superCarStatus &= ~Turn;
  superCarStatus |= Right;
}
 
void left()
{
  digitalWrite(RIGHT_MICRO_OUT, LOW);
  digitalWrite(LEFT_MICRO_OUT, HIGH);
  superCarStatus &= ~Turn;
  superCarStatus |= Left;
}
 
 
void mode(byte m)
{
  if (m == RcMode)
  {
    digitalWrite(MICRO_L_RC_H, HIGH);
    superCarStatus &= ~Mode;
    superCarStatus |= RcMode;
  }
  else
  {
    digitalWrite(MICRO_L_RC_H, LOW);
    superCarStatus &= ~Mode;
    superCarStatus |= MicroMode;
  }
}
 
void microMode()
{
  mode(MicroMode);
}
 
void rcMode()
{
  mode(RcMode);
}
 
void rcPassThru()
{
  switch( isRcDrive() )
  {
    case Forward:
      forward();
      break;
    case Backward:
      backward();
      break;
    default:
      stop();
  }
 
  switch( isRcTurn() )
  {
    case Right:
      right();
      break;
    case Left:
      left();
      break;
    default:
      straight();
  }
}
 
void resetIndicator()
{
 
  ledStrip.setPixelColor(0, 255, 0, 0);
  ledStrip.show();
  delay(500);
  ledStrip.setPixelColor(1, 0, 255, 0);
  ledStrip.show();
  delay(500);
  ledStrip.setPixelColor(2, 0, 0, 255);
  ledStrip.show();
  delay(500);
  ledStrip.setPixelColor(3, 255, 255, 255);
  ledStrip.show();
  delay(500);
 
  ledStrip.setPixelColor(0, 0, 0, 0);
  ledStrip.show();
  delay(500);
  ledStrip.setPixelColor(1, 0, 0, 0);
  ledStrip.show();
  delay(500);
  ledStrip.setPixelColor(2, 0, 0, 0);
  ledStrip.show();
  delay(500);
  ledStrip.setPixelColor(3, 0, 0, 0);
  ledStrip.show();
}
 
void superCarSetup()
{
 
  // Configure fixed pin directions
  // FWD_RC_IN is an analog input only
  // BACK_RC_IN is an analog input only
  pinMode(RIGHT_RC_IN, INPUT);
  pinMode(LEFT_RC_IN, INPUT);
  pinMode(FWD_MICRO_OUT, OUTPUT);
  pinMode(BACK_MICRO_OUT, OUTPUT);
  pinMode(RIGHT_MICRO_OUT, OUTPUT);
  pinMode(LEFT_MICRO_OUT, OUTPUT);
  pinMode(MICRO_L_RC_H, OUTPUT);
  pinMode(WheelLights, OUTPUT);
  
  pinMode(NANO_LED, OUTPUT);
  
  superCarStatus = 0x00;
 
  rcMode();
  stop();
 
  ledStrip.begin();
  ledStrip.show();
 
  resetIndicator();
}
 
void setup()
{
  superCarSetup();
  microMode();
}
 
void loop()
{
  rcPassThru();    
 
  delay(50);                      
  unsigned int distance = frontSonar.ping_cm();
  if ((distance < 10) && (distance > 0))
  {
    digitalWrite(NANO_LED, HIGH);
    digitalWrite(WheelLights, HIGH);
  }
  else
  {
    digitalWrite(NANO_LED, LOW);
    digitalWrite(WheelLights, LOW);
  }
  for (unsigned int rangeLed = 0; rangeLed < 4; rangeLed++)
  {
    ledStrip.setPixelColor(rangeLed, 0, 0, 0);
  }
  for (unsigned int rangeLed = 0; rangeLed < distance / 5; rangeLed++)
  {
    ledStrip.setPixelColor(rangeLed, 255, 0, 0);
  }
  ledStrip.show();
}

Moderator edit: code tags corrected.
« Last Edit: September 23, 2013, 02:21:04 pm by AWOL » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please be more specific than "messed up".
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

Did you pick up the RoboTech SuperCar at World Maker Faire?

If so, please contact support@robotechcenter.org for help.

This code is specific to the SuperCar configuration...

--Jay
Logged

Pages: [1]   Go Up
Jump to: