Arduino CarControl v.03

You can either download the file directly or copy paste. You can do with the code anything you want to. These files are associated with Drive a Lamborghini With Your Keyboard.

Arduino file: Car.ino

Python file: CarControl_v03.py

Arduino code:

<pre>// Car Control v. 0.2

int reversePin = 9;
int forwardPin = 8;
int leftPin = 7;
int rightPin = 6;

int order = 55;
int time = 75;

//control flag
int flag = 0;

void forward(int time){
  Serial.println("This is forward...");
  digitalWrite(forwardPin, LOW);
  delay(time);
  digitalWrite(forwardPin,HIGH);
}

void reverse(int time){
  Serial.println("This is reverse...");
  digitalWrite(reversePin, LOW);
  delay(time);
  digitalWrite(reversePin,HIGH);
}

void left(int time){
  Serial.println("This is left...");
  digitalWrite(leftPin, LOW);
  delay(time);
  digitalWrite(leftPin,HIGH);
}

void right(int time){
  Serial.println("This is right...");
  digitalWrite(rightPin, LOW);
  delay(time);
  digitalWrite(rightPin,HIGH);
}

void leftTurnForward(int time){
  digitalWrite(forwardPin, LOW);
  digitalWrite(leftPin, LOW);
  delay(time);
  off();
}

void rightTurnForward(int time){
  digitalWrite(forwardPin, LOW);
  digitalWrite(rightPin, LOW);
  delay(time);
  off();
}

void leftTurnReverse(int time){
  digitalWrite(reversePin, LOW);
  digitalWrite(leftPin, LOW);
  delay(time);
  off();
}

void rightTurnReverse(int time){
  digitalWrite(reversePin, LOW);
  digitalWrite(rightPin, LOW);
  delay(time);
  off();
}

void demoOne(){

  int demoTime = 500;

  forward(demoTime);
  reverse(demoTime);
  left(demoTime);
  right(demoTime);
}

void demoTwo(){

  int demoTime = 500;

  rightTurnForward(demoTime);
  leftTurnForward(demoTime);
  rightTurnReverse(demoTime);
  leftTurnReverse(demoTime);
}

void off(){
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
}

void orderControl(int order, int time){
  switch (order){

     //off order
     case 0: off(); break;

     //demo modes
     case 1: demoOne(); order=0; break;
     case 2: demoTwo(); order=0; break;
     case 3: demoOne(); demoTwo(); order=0; break;

     //movment options
     case 11: forward(time); order=0; break;
     case 12: reverse(time); order=0; break;
     case 13: right(time); order=0; break;
     case 14: left(time); order=0; break;

     //complex movment
     case 21: rightTurnForward(time); order=0; break;
     case 22: leftTurnForward(time); order=0; break;
     case 23: rightTurnReverse(time); order=0; break;
     case 24: leftTurnReverse(time); order=0; break;

     //no match...
     default: Serial.print("\nINVALID ORDER!... TURNING OFF!\n");
    }
}

void setup() {
  // initialize the digital pins as an output.
  pinMode(rightPin, OUTPUT);
  pinMode(leftPin, OUTPUT);
  pinMode(forwardPin, OUTPUT);
  pinMode(reversePin, OUTPUT);

  Serial.begin(115200);
  Serial.print("\n\nStart...\n");
}

void loop() {

  //Turn everything off...
  off();

  //get input
  if (Serial.available() > 0){
    order = Serial.read() - 65;
    Serial.print("I received: ");
    Serial.println(order);
    flag = 1;
  }

  if(flag){
    //complete orders
    orderControl(order,time);
  }

}

Python code:


import serial;
import pygame;
import os;
from pygame.locals import *;

def clear_screen():
	os.system('clear')

def getOrder(run):
        for event in pygame.event.get():
            if (event.type == KEYDOWN):
                keyinput = pygame.key.get_pressed();

                #complex orders
                if keyinput[pygame.K_UP] and keyinput[pygame.K_RIGHT]:
                    run[1] = 21;
                elif keyinput[pygame.K_UP] and keyinput[pygame.K_LEFT]:
                    run[1] = 22;
                elif keyinput[pygame.K_DOWN] and keyinput[pygame.K_RIGHT]:
                    run[1] = 23;
                elif keyinput[pygame.K_DOWN] and keyinput[pygame.K_LEFT]:
                    run[1] = 24;

                #simple orders
                elif keyinput[pygame.K_UP]:
                    run[1] = 11;
                elif keyinput[pygame.K_DOWN]:
                    run[1] = 12;
                elif keyinput[pygame.K_RIGHT]:
                    run[1] = 13;
                elif keyinput[pygame.K_LEFT]:
                    run[1] = 14;

                elif keyinput[pygame.K_1]:
                	run[1] = 1;
                elif keyinput[pygame.K_2]:
                	run[1] = 2;
                elif keyinput[pygame.K_3]:
                	run[1] = 3;

                #exit
                elif keyinput[pygame.K_x] or keyinput[pygame.K_q]:
                    print 'exit';
                    run[0] = False;
                    run[1] = 0;

            elif event.type == pygame.KEYUP:

                #single key
                if (run[1] < 20):
                    run[1] = 0;

                #up-right
                elif (run[1] == 21):
                    if event.key == pygame.K_RIGHT:
                        run[1] = 11;
                    elif event.key == pygame.K_UP:
                        run[1] = 13;

                #up-left
                elif (run[1] == 22):
                    if event.key == pygame.K_LEFT:
                        run[1] = 11;
                    elif event.key == pygame.K_UP:
                        run[1] = 14;

                #back-right
                elif (run[1] == 23):
                    if event.key == pygame.K_RIGHT:
                        run[1] = 12;
                    elif event.key == pygame.K_DOWN:
                        run[1] = 13;

                #back-left
                elif (run[1] == 24):
                    if event.key == pygame.K_LEFT:
                        run[1] = 12;
                    elif event.key == pygame.K_DOWN:
                        run[1] = 14;

        return run;

def main():

    clear_screen()

    print '\nStarting CarControl v.0.3\n';

    ser = serial.Serial('/dev/tty.usbmodem411', 115200, timeout=1);

    pygame.init();
    run = [True,0];
    previous = -1

    while run[0]:
        run = getOrder(run);

        #debug
        #print 'current orders: ' + str(run[1]);

        if (run[1] != previous):
            previous = run[1];
            ser.write(chr(run[1] + 65));
            print run[1];

    ser.close();

    exit('\nGoodbye!\n')

if __name__ == "__main__":
	main()

  • Fareed

    i get "Traceback (most recent call last):

    File "", line 1, in

    import serial;

    ImportError: No module named serial" when i press enter at python

  • Felipe

    how to install in windows 7 PySerial I have the python 3.3, please help me I only need that

    • CptDeadBones

      Have you tried to google some help? I do not have Windows 7. Nor do I have Python 3.3.
      Captain DeadBones
      thelivingpearl.com