Core Lab Week3 Assignment(A New Game)

The name of object: Super flash
Before a game starts:
Waiting for players to reset the instrument by pressing the reset button. The four blue and yellow LED lights are turned on.
During the game:
If the yellow LED light turns on when the player presses the button, then the player scores one point. After pressing the game button when then light flashes in the middle of the 5 lights, the buzzer also makes a sound, and the game will speed up and continue.

If the blue LED lights are turned on when the player presses the button, it indicates that the game is over.

code:

int Led1Pin = 2; //The pin of the first LED.
int Led2Pin = 3; //The pin of the second LED.
int Led3Pin = 4; //etc.
int Led4Pin = 5;
int Led5Pin = 6;

int ButtonPin = 8; //The pin of the button.

int BuzzerPin = 9;//The pin of the buzzer.

//The state of the button the last time we checked.
boolean old_val = LOW;

//Stores which LED is on.
int LightPosition = 0;

//How much time in between LED light changes.
int pause = 1000;

//What time it was when we last moved the light.
long lastMove = millis();

void setup()
{
pinMode(Led1Pin, OUTPUT);
pinMode(Led2Pin, OUTPUT);
pinMode(Led3Pin, OUTPUT);
pinMode(Led4Pin, OUTPUT);
pinMode(Led5Pin, OUTPUT);
pinMode(ButtonPin, INPUT);

Serial.begin(9600);

//Start a new game.
newGame();
}

void loop()
{
//Move the light.
if (millis() – lastMove >= pause)
{
lastMove = millis(); //Remember when we switched LED

LightPosition++; //increment the Light position.
if (LightPosition >= 6) LightPosition = 1;

move(LightPosition);//Update the light position.
}

//When the player presses the button…
if (digitalRead(ButtonPin) == HIGH && old_val == LOW)
{
//If the pressed it when the light was in the middle, speed up and continue.
if (LightPosition == 3)
{
digitalWrite(Led3Pin, LOW);
delay(50);
digitalWrite(Led3Pin, HIGH);

tone(BuzzerPin, 2470);
delay(1000);
tone(BuzzerPin, 3295);
delay(1000);
noTone(BuzzerPin);

//Speed up the game.
if (pause > 700)
{
pause -= 100;
} else if (pause > 500)
{
pause -= 50;
} else if (pause > 300)
{
pause -= 25;
} else if (pause > 10)
{
pause -= 10;
} else if (pause > 1)
{
pause -= 1;
}

Serial.print(“Score: “);
Serial.println(pause);

} else //If the pressed it at the wrong time, show their final score and start a new game.
{
//Game over
Serial.println(“GAME OVER”);
Serial.print(“Final Score “);
Serial.println(pause);

//Blink the Led that the player stopped on.
for (int x = 0; x <= 10; x++)
{
if (digitalRead(LightPosition + 1) == LOW)
{
digitalWrite(LightPosition + 1, HIGH);
}
else
{
digitalWrite(LightPosition + 1, LOW);
}
delay(200);
}

//Show a LED bar based on how well the player did.
digitalWrite(Led1Pin, HIGH);
delay(500);
if (pause < 800)
{
digitalWrite(Led2Pin, HIGH);
delay(500);
}
if (pause < 600)
{
digitalWrite(Led3Pin, HIGH);
delay(500);
}
if (pause < 250)
{
digitalWrite(Led4Pin, HIGH);
delay(500);
}
if (pause < 100)
{
digitalWrite(Led5Pin, HIGH);
delay(500);
}
delay(3000);

newGame();
}
}

old_val = digitalRead(ButtonPin);

}

//Updates the light’s position.
void move(int LightPosition)
{
//Turn off all LEDs
for (int x = Led1Pin; x <= Led5Pin; x++)
{
digitalWrite(x, LOW);
}

//Turn on the LED
digitalWrite(LightPosition + 1, HIGH);
}

void newGame()
{
LightPosition = 0;
pause = 1000;
Serial.println(“New Game: Score 1000”);
}

Core lab week1 assignment

Arduino week1 assignment

 

  1. i) Types of connection to the Arduino

 

  • Uno R3 Controller Board 1PC

The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing).

 

  • LCD 1602 Module (with pin header 1PC)

16×2 Character LCD module based on the HD44780 controller.

White characters on blue background with backlight.

Comes with headers, a diode and a variable resistor for your peace of mind.

 

  • Prototype Expansion Board 1PC

The ProtoShield is prototype expansion board with 2 LEDs and 2 button, which can be used directly and more convenience. All the pins and power have been lead out. It is very suitable for building prototype circuit with Arduino.

 

  • Power Supply Module 1PC

This module has power-down stored function and can store 10 groups preset value.

 

  • ULN2003 Stepper Motor Diver Board 1PC

This is a convenient package of seven Darlington transistors.

 

  • Stepper Motor 1PC

This is a ready-to-go cable and a machined drive shaft (so you can easily attach stuff). We drove it with an Adafruit motor shield for Arduino and it hummed along nicely at 50 RPM.

 

  • Servo Motor (SG90)1PC

core motor

 

  • 5v Relay 1PC

5V Relay Module 4-Channel.This is a 5V 4-Channel Relay interface board. Be able to control various appliances, and other equipment with large current. Indication LED’s for Relay output status.

 

  • IR Receiver Module 1PC

Arduino Mini infrared wireless remote control kit consists of 38KHz infrared remote control and infrared receiver modules, Mini IR can transmit distances of up to 8 m

 

  • Joystick Module 1PC

The kind of program that we need to monitor the joystick has to make a polling to two of the analog pins.

 

  • DHT 11 Temperature and Humidity Module 1PC

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed).

 

  • Ultrasonic Sensor 1PC

The HRLV-MaxSonar-EZ sensor line is the fastest way to get precision range-finding into your computer.

 

  • 3-6v Motor 1PC

electric scooter motor.

 

  • Active Buzzer 1PC

An active buzzer will generate a tone using an internal oscillator, so all that is needed is a DC voltage

 

  • Tilt Switch 1PC

Tilt sensors are switches that can detect basic motion/orientation.

 

  • 74HC5951PC

These chips are DIP package so you can easily plug them into any breadboard or perfboard with 0.1″ spacing. The digital outputs are good for about 20mA, which makes them ideal for LEDs or driving power transistors.

 

  • Button(Small)5PCS

 

  • Potentiometer 1PC

Manufactured by Spectra Symbol, these are nice little ribbon controllers (also known as ‘soft potentiometers’) with an adhesive backing.

 

  • 1 Digit 7-Segment Display 1PC

this a single LED.

 

  • 4 Digit 7-segment Display 1PC

this a single LED.

 

  • Passive Buzzer 1PC

An active buzzer will generate a tone using an internal oscillator, so all that is needed is a DC voltage

 

  • Remote 1PC

Remote controller.

 

  • Breadboard 1PC

These nice switches are perfect for use with breadboard and perfboard projects. They have 0.1″ spacing and snap in nicely into a solderless breadboard.

 

  • USB Cable 1PC

This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your NETduino.

 

  • Female-to-male Dupont wire 10 PCS

Having different colors they greatly facilitate their recognition within a prototype (often a tangle of wires) so that they can be classified by function: red 5V, black GROUND, yellow PWM signal and so on.

 

  • 65 Jumper Wire 1PC

For bread-boarding with unusual non-header-friendly surfaces, these cables will be your best friends! No longer will you have long strands of alligator clips that are grabbing little wires.

 

  • Battery with DC 1PC

battery wire

 

  • 9v Battery 1PC

Battery

 

  • Resistor 120PCS

handy resistor packs

 

  • LED 25PCS

LED light ball

 

  • RGB 1PC

Color LED light ball

 

  • Thermistor 1PC

This epoxy-coated precision 1% 10K thermistor is an inexpensive way to measure temperature in weather or liquids. T

 

  • Diode Rectifier 1N4007 2PCS

put it between your DC power jack and circuitry to avoid a negative-voltage that would zap your delicate electronics

 

  • Photoresistor (Photocell)2PCS

CdS cells are little light sensors. As the squiggly face is exposed to more light, the resistance goes down. When its light, the resistance is about 5-10KΩ, when dark it goes up to 200KΩ.

 

  • NPN Transistor PN22222PCS

NPN transistors whenever we need to control medium-power electronics such as small motors, solenoids, or IR LEDs.

 

ii)A sample Fritzing schematic

iii)function or library is needed to make it work

 

Blink

 

Turns an LED on for one second, then off for one second, repeatedly.

 

Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO

it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to

the correct LED pin independent of which board is used.

If you want to know what pin the on-board LED is connected to on your Arduino

model, check the Technical Specs of your board at:

https://www.arduino.cc/en/Main/Products

 

modified 8 May 2014

by Scott Fitzgerald

modified 2 Sep 2016

by Arturo Guadalupi

modified 8 Sep 2016

by Colby Newman

 

This example code is in the public domain.

 

http://www.arduino.cc/en/Tutorial/Blink

*/

 

// the setup function runs once when you press reset or power the board

void setup() {

// initialize digital pin LED_BUILTIN as an output.

pinMode(LED_BUILTIN, OUTPUT);

}

 

// the loop function runs over and over again forever

void loop() {

digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)

delay(1000);                       // wait for a second

digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

delay(1000);                       // wait for a second

}