Arduino TSP RC Car


Made With:


Taking the Arduino, Dallas Semiconductor one-wire temperature sensor, the Arduino Motor sheild, and a bluetooth Module. Let us make a Temperature Sensing Car!!!


As Seen at the Vermont Code Camp 2012

Step 1

Load the following code into your Arduino:

/*************************************************************
Remote Control Car W/ 1 wire
BY: Chris Yarger

*************************************************************/
// load 1 wire libs
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 6 on the Arduino

#define ONE_WIRE_BUS 6

// Setup a oneWire instance to communicate with any OneWire devices

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 

DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress insideThermometer = { 0x28, 0x41, 0xB8, 0x4D, 0x03, 0x00, 0x00, 0xB4 };

//Initialize Drive Pins

int ADir = 12;
int ABrake = 9;
int ASpin = 3;
int Aout = 0; 
//initialize Steering Pins
int BDir = 13;
int BBrake = 8;
int BSpin = 11;
int Bout = 1;
// Initialize input
int incomingByte;      // a variable to read incoming serial data into
int Aout1 = 999;
int Bout1 = 999;
int i = 22;



void setup(void) {
  
  //Setup Channel A
  pinMode(ADir, OUTPUT); //Initiates Motor Channel A pin
  pinMode(ABrake, OUTPUT); //Initiates Brake Channel A pin
  pinMode(Aout, INPUT);   // digital sensor is on digital pin 2
  // Setup Chanel B  
  pinMode(BDir, OUTPUT); //Initiates Motor Channel A pin
  pinMode(BBrake, OUTPUT); //Initiates Brake Channel A pin
  pinMode(Bout, INPUT);   // digital sensor is on digital pin 2
//set pins for input
  Serial.begin(9600);
    // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);
}



void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}



void loop(){
    if (i == 22) {
 Serial.write(27);   //Print "esc"
 Serial.print("[2J");
 Aout1 = analogRead(Aout)/4;
 Bout1 = analogRead(Bout)/4;
 Serial.print("Aout=");  
 Serial.println(Aout1);  
 Serial.print("Bout=");  
 Serial.println(Bout1);

 sensors.requestTemperatures();
  
 Serial.print("Inside temperature is: ");
 printTemperature(insideThermometer);
 delay(1000); 
          }



//Parse Serial data in to motor actions

  //Dir HIGH = Forward
  //Dir LOW = Reverse
  //forward @ full speed

if (Serial.available() > 0) {
   // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();



    if (incomingByte == 'W') {
  digitalWrite(ADir, HIGH); //Establishes forward direction of Channel A
  digitalWrite(ABrake, LOW);   //Disengage the Brake for Channel A
  analogWrite(ASpin, 2000);   //Spins the motor on Channel A at full speed
                                       }


     if (incomingByte == 'S') {
  digitalWrite(ADir, LOW); //Establishes forward direction of Channel A
  digitalWrite(ABrake, LOW);   //Disengage the Brake for Channel A
  analogWrite(ASpin, 2000);   //Spins the motor on Channel A at full speed
                                       }

 

    
    //Steering Wheel control

         if (incomingByte == 'D') {
 digitalWrite(BDir, LOW); //Establishes forward direction of Channel B
 digitalWrite(BBrake, LOW);   //Disengage the Brake for Channel B
 analogWrite(BSpin, 500);   //Spins the motor on Channel B 
//Pauses for next command long enough to turn steering wheels 1/2 turn
delay(425);
// Engages Brake for channel B
 digitalWrite(BBrake, HIGH);   //Engage the Brake for Channel B
 analogWrite(BSpin, 0);   //Stops spins on motor B
 
                                            }
      
   if (incomingByte == 'A') {
 digitalWrite(BDir, HIGH); //Establishes forward direction of Channel B
 digitalWrite(BBrake, LOW);   //Disengage the Brake for Channel B
 analogWrite(BSpin, 500);   //Spins the motor on Channel B 
//Pauses for next command long enough to turn steering wheels 1/2 turn
delay(425);
// Engages Brake for channel B
 digitalWrite(BBrake, HIGH);   //Engage the Brake for Channel B
 analogWrite(BSpin, 0);   //Stops spins on motor B
                                    }

//Emergency Brakes
          if (incomingByte == 'Q') {
 digitalWrite(BBrake, HIGH);   //Engage the Brake for Channel B
 analogWrite(BSpin, 0);   //Stops spins on motor B
 digitalWrite(ABrake, HIGH);   //Engage the Brake for Channel A
 analogWrite(ASpin, 0);   //Stops spins on motor A
                                            }
         }

}



Version 1.4 of this code Utilizes case Statements rather than If/then loops.


Assembly Steps:



To be continued.....
Arduino TSP RC Car Arduino TSP RC Car Reviewed by Chris Yarger on 21:53 Rating: 5

No comments:

© 2012 cpyarger.com License: (cc) BY-NC-SA. Template images by enot-poloskun. Powered by Blogger.