Skip to main content

simple car game


simple car game this is a simple car game based on arduino and 16x2 lcd screen
easy to make but take care for the wiring of lcd to arduino

let's make




material needed:-

  • Arduino uno
  • 16x2 lcd
  • battery
  • 10k potentiometer(2 pcs.)
  • switch (needed for backlight)
  • jumper wire
  • breadbord(not necessary)



instructions:-


  • first connect arduino 5v & gnd to breadboard

  • _________________________________________________________________________________________

  • connect lcd to arduino


    you should connect arduino to lcd
    connections are as follow from left to right
    • lcd--->arduino
    • 1--->gnd
    • 2--->5v
    • 3--->potentiometer(as shown in picture)
    • 4--->D8
    • 5--->gnd
    • 6--->D9
    • 7--->leave
    • 8--->leave
    • 9--->leave
    • 10--->leave
    • 11--->D6
    • 12--->D5
    • 13--->D4
    • 14--->D3
    • 15--->5v(using switch)
    • 16--->gnd
  • ________________________________________________________________________________________
  • connect potentiometer to A1



    do as shown in image it will use to control the game
  • ________________________________________________________________________________________
  • upload the code on the arduino
/*  Simple Car game   
 *  by AREEBURRUB
 *  Website   http://areeburrub.page.tl
 */
#include <LiquidCrystal.h>
// LiquidCrystal display
// You can use any Hitachi HD44780 compatible. Wiring explained at
// http://www.arduino.cc/en/Tutorial/LiquidCrystal
LiquidCrystal lcd(8, 9, 6, 5, 4, 3);

// Steering wheel potentiometer
const int POTPIN = A1;
const int MAXPOT = 690; // no need to turn the wheel all the way to 1023 :)

// Piezo speaker
const int SPEAKERPIN = A9;

const int RANDSEEDPIN = A0; // an analog pin that isn't connected 
                            //to anything it is used for more random result

const int MAXSTEPDURATION = 300; // Start slowly, each step is 1 millisec shorter.
const int MINSTEPDURATION = 150; // This is as fast as it gets

const int NGLYPHS = 6;
// the glyphs will be defined starting from 1 (not 0),
// to enable lcd.print() of null-terminated strings
byte glyphs[NGLYPHS][8] = {
  // 1: car up
  { B00000,
    B01110,
    B11111,
    B01010,
    B00000,
    B00000,
    B00000,
    B00000}
  // 2: car down
  ,{B00000,
    B00000,
    B00000,
    B00000,
    B01110,
    B11111,
    B01010,
    B00000}
  // 3: truck up
  ,{B00000,
    B11110,
    B11111,
    B01010,
    B00000,
    B00000,
    B00000,
    B00000}
  // 4: truck down
  ,{B00000,
    B00000,
    B00000,
    B00000,
    B11110,
    B11111,
    B01010,
    B00000}
  // 5: crash up
  ,{B10101,
    B01110,
    B01110,
    B10101,
    B00000,
    B00000,
    B00000,
    B00000}
  // 6: crash down
  ,{B00000,
    B00000,
    B00000,
    B10101,
    B01110,
    B01110,
    B10101,
    B00000}
};

const int NCARPOSITIONS = 4;

// Each position is mapped to a column of 2 glyphs
// Used to make sense when I had a 5th position
// where car or crash was drawn as 2 glyphs
// (can't do that since 0 terminates strings),
// so it's kinda silly now, but it ain't broke :)
const char BLANK=32;
char car2glyphs[NCARPOSITIONS][2] = {
  {1,BLANK},{2,BLANK},{BLANK,1},{BLANK,2}
};
char truck2glyphs[NCARPOSITIONS][2] = {
  {3,BLANK},{4,BLANK},{BLANK,3},{BLANK,4}
};
char crash2glyphs[NCARPOSITIONS][2] = {
  {5,BLANK},{6,BLANK},{BLANK,5},{BLANK,6}
};

const int ROADLEN = 15; // LCD width (not counting our car)
int road[ROADLEN]; // positions of other cars
char line_buff[2+ROADLEN]; // aux string for drawRoad()
int road_index;
int car_pos;
// Off-the-grid position means empty column, so MAXROADPOS
// determines the probability of a car in a column
// e.g. 3*NCARPOSITIONS gives p=1/3
const int MAXROADPOS = 3*NCARPOSITIONS;
int step_duration;

int crash; // true if crashed
unsigned int crashtime; // millis() when crashed
const int CRASHSOUNDDURATION = 250;
const char *INTRO1="Trucks by Areeb-ur-Rub";//don't change it 

const int INTRODELAY = 2000;//don't change it 

void setup()
{
  crash = crashtime = road_index = 0;
  step_duration = MAXSTEPDURATION;
  line_buff[1+ROADLEN] = '\0'; // null terminate it
  randomSeed(analogRead(RANDSEEDPIN));
  for (int i=0; i<NGLYPHS; i++) {
    lcd.createChar(i+1,glyphs[i]);
  }
  for (int i=0; i<ROADLEN; i++) {
    road[i]=-1;
  }
  pinMode(SPEAKERPIN,OUTPUT);
  analogWrite(SPEAKERPIN,0); // to be on the safe side
  lcd.begin(16,2);
  getSteeringWheel();
  drawRoad();
  lcd.setCursor(1,0);
  lcd.print(INTRO1);
  for (int positionCounter = 0; positionCounter < 40; positionCounter++) {
  // scroll one position right:
  lcd.scrollDisplayLeft();
  // wait a bit:
    delay(250);
  }
  delay(INTRODELAY);
}

void loop() {
  unsigned long now = millis()-INTRODELAY;
  
  if (!crash) {
    drawRoad();
    getSteeringWheel();
    crash = (car_pos==road[road_index]);  
      
  }
  if (crash) {
    if (!crashtime) {
      crashtime=now;
      // Game over text
      // (keep first 2 "crash" columns intact)
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Crash in");
      lcd.setCursor(9,0);
      lcd.print(now/1000);
      lcd.print("sec");
    }
    if ((now-crashtime)<CRASHSOUNDDURATION) {
      analogWrite(SPEAKERPIN,random(255)); // white noise
    } 
    else {
      analogWrite(SPEAKERPIN,0); // dramatic post-crush silence :)   
    }
    delay(10); // Wait a bit between writes
  } 
  else {

    int prev_pos = road[(road_index-1)%ROADLEN];
    int this_pos = random(MAXROADPOS);
    while (abs(this_pos-prev_pos)<2) { // don't jam the road
      this_pos = random(MAXROADPOS);
    }
    road[road_index] = this_pos;
    road_index = (road_index+1)%ROADLEN;
    drawRoad();
    delay(step_duration);
    if (step_duration>MINSTEPDURATION) {
      step_duration--; // go faster
    }
  }
}
void getSteeringWheel() {
  car_pos = map(analogRead(POTPIN),0,1023,0,NCARPOSITIONS);
}

void drawRoad() {
  for (int i=0; i<2; i++) {
    if (crash) {
      line_buff[0]=crash2glyphs[car_pos][i];
    } 
    else {
      line_buff[0]=car2glyphs[car_pos][i];
    }
    for (int j=0; j<ROADLEN; j++) {
      int pos = road[(j+road_index)%ROADLEN];
      line_buff[j+1] = pos>=0 && pos<NCARPOSITIONS ? truck2glyphs[pos][i] : BLANK;
    }
    lcd.setCursor(0,i);
    lcd.print(line_buff);
  }
}

__________________________________________________________________________________________

check it out how I did it


Comments

Popular posts from this blog

diy relay module

I If you want a relay module and you are thinking that you will buy it online here is a link Amazon.com : relay module
but as you see it is expensive i will tell you a very cheap DIY

material needed:-

vero board/prototyping boardrelay 12vdiode 40001 or 40007bc558 transistor9volt batterymale header pin or simple wire for connectingstep1:-
place all the thing on the veroboard as shown in picture or in schematic or in video
breadboard view
schematic picture


step2:-
connect signal to desired pin of arduino 
and ground to ground pin of arduino
and don't forget to connect 9volt battery

 finished


if it is not working check the diode position and transistor position and also see ground connections

Mosquito repeller

I nowadays mosquito is the huge problem and mosquito is also spreading many diseases and this DIY will really help you 

Here is a list of the components and tools required
Arduino UnoPCBPiezoelectric DiskHeader PinsWiresSoldering IronSoldering Leaddo as shown in video

upload the code from here
/* * this is an arduino project * for those who is getting sucked by mosquito */intspeaker="9";//first let set the pin for pizo diskintfrequency="31000";/*now lets keep the frequency 31000hz * which irritate the mosquito */voidsetup(){pinMode(9,OUTPUT);//set pin 9 as output}voidloop(){tone(speaker,frequency,1000);//use tone command to set frequency/*   * if you dont know about " tone "   * select tone and press   * Crtl + Shift + F   */}

pov display

pov display here is an exciting project for you all pov display this project will
float words in air and easy to made


material needed:-
Arduino nano (but can use any arduino) 5 leds 5 220 ohms resistor jumper wire battery vero/prototyping board switch (if needed)


instructions:-
first connect leds to vero/prototyping board
connect the led to vero/prototyping board be careful that the led should be connected to
digital pins 8,9,10,11,12 of arduino _________________________________________________________________________________________
after fitting leds to vero/prototyping board cut the excess board
cut it by using a sharp knife or scissor. ________________________________________________________________________________________ upload the code on the arduino // how long each led stays on forintdelayTime=4;//how long between each intcharBreak=3;//how long to wait after the message is finished before it repeatsintresetTime=20;intLED1=8;intLED2=9;intLED3=10;intLED4=11;intLED5=12;…