I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED strips and 3 - ultrasonics. I have them working independently but I need to combine the code so they work together. Here is what I have:
Memory Game
#define PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s byte sequence[100]; // Storage for the light sequence byte curLen = 0; // Current length of the sequence byte inputCount = 0; // The number of times that the player has pressed a (correct) button in a given turn byte lastInput = 0; // Last input from the player byte expRd = 0; // The LED that's suppose to be lit by the player bool btnDwn = false; // Used to check if a button is pressed bool wait = false; // Is the program waiting for the user to press a button bool resetFlag = false; // Used to indicate to the program that once the player lost byte soundPin = 5; // Speaker output byte noPins = 4; // Number of buttons/LEDs (While working on this, I was using only 2 LEDs) // You could make the game harder by adding an additional LED/button/resistors combination. byte pins[] = {2, 13, 10, 8}; // Button input pins and LED ouput pins - change these vaules if you wwant to connect yourbuttons to other pins // The number of elements must match noPins below long inputTime = 0; // Timer variable for the delay between user inputs void setup() { delay(3000); // This is to give me time to breathe after connection the arduino - can be removed if you want Serial.begin(9600); // Start Serial monitor. This can be removed too as long as you remove all references to Serial below Reset(); } /// /// Sets all the pins as either INPUT or OUTPUT based on the value of 'dir' /// void setPinDirection(byte dir){ for(byte i = 0; i < noPins; i++){ pinMode(pins[i], dir); } } //send the same value to all the LED pins void writeAllPins(byte val){ for(byte i = 0; i < noPins; i++){ digitalWrite(pins[i], val); } } //Makes a (very annoying :) beep sound void beep(byte freq){ analogWrite(soundPin, 2); delay(freq); analogWrite(soundPin, 0); delay(freq); } /// /// Flashes all the LEDs together /// freq is the blink speed - small number -> fast | big number -> slow /// void flash(short freq){ setPinDirection(OUTPUT); /// We're activating the LEDS now for(int i = 0; i < 5; i++){ writeAllPins(HIGH); beep(50); delay(freq); writeAllPins(LOW); delay(freq); } } /// ///This function resets all the game variables to their default values /// void Reset(){ flash(500); curLen = 0; inputCount = 0; lastInput = 0; expRd = 0; btnDwn = false; wait = false; resetFlag = false; } /// /// User lost /// void Lose(){ flash(50); } /// /// The arduino shows the user what must be memorized /// Also called after losing to show you what you last sequence was /// void playSequence(){ //Loop through the stored sequence and light the appropriate LEDs in turn for(int i = 0; i < curLen; i++){ Serial.print("Seq: "); Serial.print(i); Serial.print("Pin: "); Serial.println(sequence[i]); digitalWrite(sequence[i], HIGH); delay(500); digitalWrite(sequence[i], LOW); delay(250); } } /// /// The events that occur upon a loss /// void DoLoseProcess(){ Lose(); // Flash all the LEDS quickly (see Lose function) delay(1000); playSequence(); // Shows the user the last sequence - So you can count remember your best score - Mine's 22 by the way :) delay(1000); Reset(); // Reset everything for a new game } /// /// Where the magic happens /// void loop() { if(!wait){ //****************// // Arduino's turn // //****************// setPinDirection(OUTPUT); // We're using the LEDs randomSeed(analogRead(A0)); // https://www.arduino.cc/en/Reference/RandomSeed sequence[curLen] = pins[random(0,noPins)]; // Put a new random value in the next position in the sequence - https://www.arduino.cc/en/Reference/random curLen++; // Set the new Current length of the sequence playSequence(); // Show the sequence to the player beep(50); // Make a beep for the player to be aware wait = true; // Set Wait to true as it's now going to be the turn of the player inputTime = millis(); // Store the time to measure the player's response time }else{ //***************// // Player's turn // //***************// setPinDirection(INPUT); // We're using the buttons if(millis() - inputTime > PLAYER_WAIT_TIME){ // If the player takes more than the allowed time, DoLoseProcess(); // All is lost :( return; } if(!btnDwn){ // expRd = sequence[inputCount]; // Find the value we expect from the player Serial.print("Expected: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above Serial.println(expRd); // Serial Monitor Output - Should be removed if you removed the Serial.begin above for(int i = 0; i < noPins; i++){ // Loop through the all the pins if(pins[i]==expRd) continue; // Ignore the correct pin if(digitalRead(pins[i]) == HIGH){ // Is the buttong pressed lastInput = pins[i]; resetFlag = true; // Set the resetFlag - this means you lost btnDwn = true; // This will prevent the program from doing the same thing over and over again Serial.print("Read: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above Serial.println(lastInput); // Serial Monitor Output - Should be removed if you removed the Serial.begin above } } } if(digitalRead(expRd) == 1 && !btnDwn) // The player pressed the right button { inputTime = millis(); // lastInput = expRd; inputCount++; // The user pressed a (correct) button again btnDwn = true; // This will prevent the program from doing the same thing over and over again Serial.print("Read: "); // Serial Monitor Output - Should be removed if you removed the Serial.begin above Serial.println(lastInput); // Serial Monitor Output - Should be removed if you removed the Serial.begin above }else{ if(btnDwn && digitalRead(lastInput) == LOW){ // Check if the player released the button btnDwn = false; delay(20); if(resetFlag){ // If this was set to true up above, you lost DoLoseProcess(); // So we do the losing sequence of events } else{ if(inputCount == curLen){ // Has the player finished repeating the sequence wait = false; // If so, this will make the next turn the program's turn inputCount = 0; // Reset the number of times that the player has pressed a button delay(1500); } } } } } }
Ultra- Sonic Code
LED LIght Strips
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson // released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> #endif // Which pin on the Arduino is connected to the NeoPixels? // On a Trinket or Gemma we suggest changing this to 1 #define PIN 6 // How many NeoPixels are attached to the Arduino? #define NUMPIXELS 16 // When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. // Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest // example for more information on possible values. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); int delayval = 500; // delay for half a second void setup() { // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket #if defined (__AVR_ATtiny85__) if (F_CPU == 16000000) clock_prescale_set(clock_div_1); #endif // End of trinket special code pixels.begin(); // This initializes the NeoPixel library. } void loop() { // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one. for(int i=0;i<NUMPIXELS;i++){ // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately bright green color. pixels.show(); // This sends the updated pixel color to the hardware. delay(delayval); // Delay for a period of time (in milliseconds). } }
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the
AdaFruit NeoPixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#dene PIN 6
// How many NeoPixels are attached to the Arduino?
#dene NUMPIXELS 16
// When we setup the NeoPixel library, we tell it how many pixels,
and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the
third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN,
NEO_GRB + NEO_KHZ800);
#dene PLAYER_WAIT_TIME 2000 // The time allowed between button presses - 2s
byte sequence[100]; // Storage for the light sequence
byte curLen = 0; // Current length of the sequence
byte inputCount = 0; // The number of times that the player has
pressed a (correct) button in a given turn
byte lastInput = 0; // Last input from the player
byte expRd = 0; // The LED that's suppose to be lit by the
player
bool btnDwn = false; // Used to check if a button is pressed
bool wait = false; // Is the program waiting for the user to press
a button
bool resetFlag = false; // Used to indicate to the program that
once the player lost
byte soundPin = 5; // Speaker output
byte noPins = 4; // Number of buttons/LEDs (While working on this,
I was using only 2 LEDs)
// You could make the game harder by adding an additional
LED/button/resistors combination.
byte pins[] = {2, 13, 10, 8}; // Button input pins and LED ouput
pins - change these vaules if you wwant to connect yourbuttons to
other pins
// The number of elements must match noPins below
long inputTime = 0; // Timer variable for the delay between user
inputs
// denes pins numbers
const int trigPin = 9;
const int echoPin = 10;
// denes variables
long duration;
int distance;
int delayval = 500; // delay for half a second
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// This is for Trinket 5V 16MHz, you can remove these three
lines if you are not using a Trinket
#if dened (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
pixels.begin(); // This initializes the NeoPixel library.
delay(3000); // This is to give me time to breathe after connection
the arduino - can be removed if you want
Serial.begin(9600); // Start Serial monitor. This can be removed
too as long as you remove all references to Serial below
Reset();
}
///
/// Sets all the pins as either INPUT or OUTPUT based on the value
of 'dir'
///
void setPinDirection(byte dir){
for(byte i = 0; i < noPins; i++){
pinMode(pins[i], dir);
}
}
//send the same value to all the LED pins
void writeAllPins(byte val){
for(byte i = 0; i < noPins; i++){
digitalWrite(pins[i], val);
}
}
//Makes a (very annoying :) beep sound
void beep(byte freq){
analogWrite(soundPin, 2);
delay(freq);
analogWrite(soundPin, 0);
delay(freq);
}
///
/// Flashes all the LEDs together
/// freq is the blink speed - small number -> fast | big number
-> slow
///
void ash(short freq){
setPinDirection(OUTPUT); /// We're activating the LEDS now
for(int i = 0; i < 5; i++){
delay(freq);
}
}
///
///This function resets all the game variables to their default
values
///
void Reset(){
ash(500);
curLen = 0;
inputCount = 0;
lastInput = 0;
expRd = 0;
btnDwn = false;
wait = false;
resetFlag = false;
}
///
/// User lost
///
void Lose(){
ash(50);
}
///
/// The arduino shows the user what must be memorized
/// Also called after losing to show you what you last sequence
was
///
void playSequence(){
//Loop through the stored sequence and light the appropriate LEDs
in turn
for(int i = 0; i < curLen; i++){
Serial.print("Seq: ");
Serial.print(i);
Serial.print("Pin: ");
Serial.println(sequence[i]);
digitalWrite(sequence[i], HIGH);
delay(500);
digitalWrite(sequence[i], LOW);
delay(250);
}
}
///
/// The events that occur upon a loss
///
void DoLoseProcess(){
Lose(); // Flash all the LEDS quickly (see Lose function)
delay(1000);
playSequence(); // Shows the user the last sequence - So you can
count remember your best score - Mine's 22 by the way :)
delay(1000);
Reset(); // Reset everything for a new game
}
///
/// Where the magic happens
///
void loop() {
if(!wait){
//****************//
// Arduino's turn //
//****************//
setPinDirection(OUTPUT); // We're using the LEDs
randomSeed(analogRead(A0));
sequence[curLen] = pins[random(0,noPins)]; // Put a new random
value in the next position in the sequence -
https://www.arduino.cc/en/Reference/random
curLen++; // Set the new Current length of the sequence
playSequence(); // Show the sequence to the player
beep(50); // Make a beep for the player to be aware
wait = true; // Set Wait to true as it's now going to be the turn
of the player
inputTime = millis(); // Store the time to measure the player's
response time
}else{
//***************//
// Player's turn //
//***************//
return;
}
if(!btnDwn){ //
expRd = sequence[inputCount]; // Find the value we expect from the
player
Serial.print("Expected: "); // Serial Monitor Output - Should be
removed if you removed the Serial.begin above
Serial.println(expRd); // Serial Monitor Output - Should be removed
if you removed the Serial.begin above
for(int i = 0; i < noPins; i++){ // Loop through the all the
pins
if(pins[i]==expRd)
continue; // Ignore the correct pin
if(digitalRead(pins[i]) == HIGH){ // Is the buttong pressed
lastInput = pins[i];
resetFlag = true; // Set the resetFlag - this means you lost
btnDwn = true; // This will prevent the program from doing the same
thing over and over again
Serial.print("Read: "); // Serial Monitor Output - Should be
removed if you removed the Serial.begin above
Serial.println(lastInput); // Serial Monitor Output - Should be
removed if you removed the Serial.begin above
}
}
}
if(digitalRead(expRd) == 1 && !btnDwn) // The player
pressed the right button
{
inputTime = millis();
lastInput = expRd;
inputCount++; // The user pressed a (correct) button again
btnDwn = true; // This will prevent the program from doing the same
thing over and over again
Serial.print("Read: "); // Serial Monitor Output - Should be
removed if you removed the Serial.begin above
Serial.println(lastInput); // Serial Monitor Output - Should be
removed if you removed the Serial.begin above
}else{
if(btnDwn && digitalRead(lastInput) == LOW){ // Check if
the player released the button
btnDwn = false;
delay(20);
if(resetFlag){ // If this was set to true up above, you lost
DoLoseProcess(); // So we do the losing sequence of events
}
else{
if(inputCount == curLen){ // Has the player nished repeating the
sequence
wait = false; // If so, this will make the next turn the program's
turn
inputCount = 0; // Reset the number of times that the player has
pressed a button
delay(1500);
}
}
}
}
}
//Ultra- Sonic Code
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in
microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
Serial.println(distance);
//LED LIght Strips
// For a set of NeoPixels the rst NeoPixel is 0, second is 1,
all the way up to the count of pixels minus one.
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 0,0,0 up to
255,255,255
pixels.setPixelColor(i, pixels.Color(0,150,0)); // Moderately
bright green color.
pixels.show(); // This sends the updated pixel color to the
hardware.
delay(delayval); // Delay for a period of time (in
milliseconds).
}
}
I am doing an Arduino Uno project where I made a "Simon says" memory game with 3 neopixel LED str...
Using the template provided in the lab details section, formulate a program for the Arduino to generate a PWM signal duty cycles between 0% and 100% at intervals of 10% where a button will increment the PWM signal. Remember the RC is 10 milliseconds. Provide a copy of the final code and explain how it works. Arduino PWM Example int Pin = 9; void setup() { pinMode (Pin, OUTPUT); } void loop() { analogWrite (Pin, 127); // Generate 50% duty...
This question is about the Arduino code. Please complete a 4-input circuit on your Arduino. Your circuit, in addition to cycling through a binary representation of the numbers 0-15 using LEDs, should use the Serial port to output the decimal equivalent of each of the numbers in the format: Output: 15 ... if the value was 1111, and so forth. You will be asked to upload your code as well as a photo of your working breadboard circuit. Add comments...
I am trying to program my Arduino UNO to implement a flashing pattern with two LEDs. When one LED is off, the other must be on. The program needs to be done in assembly. I have a working program in c++ but I need to convert it to assembly. Here is my c++ program: int led = 13; int led2 = 12; void setup() { pinMode(led, OUTPUT); pinMode(led2, OUTPUT); } void loop() { digitalWrite(led, HIGH); delay(1000); digitalWrite(led, LOW);...
I have a program for Arduino written such that when I hold a button down, it will turn an LED off. I want to be able to print to the serial monitor how long the button was pressed for after I let go of the button. Does anybody have any ideas? Below is the code I have written thus far: Text of Code: #define LED RED_LED #define buttonPIN PUSH2 const int buttonPin = PUSH2; // the number of the pushbutton...
In the code below i am using a LS-3006 SERVO on the arduino uno and everytime i hit the pushbutton to input a time the servo comes on. Would anyone be able to tell me what a solution is to this problem? Thanks #include <Wire.h> #include <DS3231.h> #include <Servo.h> #include <LiquidCrystal.h> #include <Keypad.h> const byte ROWS = 4; // # of rows const byte COLS = 4; // # of columns // Define the Keymap char keys[ROWS][COLS] = { {'1','2','3','A'},...
I'm trying to make a circuit from this code that I found for a personal project, but I'm having trouble figuring it out. Could someone make me a Circuit Diagram for this code with an LED, Servo, and Arduino Nano on BreadBoard. (No Battery, just use usb power from Nano) Thank You. Nano Code: const int servoPin = 5; const int buttonPin = 3; const int LEDPin = 4; #include <Servo.h> Servo visorServo; void setup() { visorServo.write(0); // Initial position...
I need help with doing these tasks for code composer Lab 3 - Branching, Push Button and LEDs-Reading Assignment in this lab, we are going to control a LED via a push button- using general purpose digital 10 for both input (button) and output (LED) on port 1 - using Code Composer Studio. Furthermore, we are going to use a branch instruction to create an IF-ELSE structure in assembly to determine if the LED should be lit up based on...