Hi,
I'm very new to Arduino so i'm having so many problems trying to code for my project.
I'm making a basic simon says game using code from an online source and adding the screen to give instructions.
(original code sourced from 'Mr. Reive' :Simon Says Arduino Wiring and Code Tutorial - YouTube
Code for Simon Says Arduino Wiring and Code Tutorial · GitHub
The main issue at the moment is that I bought an LCD screen with a pre soldered 4 pin backpack on it. For this it was suggested that I downloaded a new library to control it. The issue is two things:
-
The code needs six pin inputs to work, when the backpack means it's only connected to two pins on the arduino. What is the solution to this?
-
The new library is causing an error, When one section (line 14) is deleted the code will compile but probably won't work since part of the library zip file is missing from the code. If this makes any sense? any help would be appreciated
The library I used is here:https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
i'll include the code i'm working on below, Thank you in advance
#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_I2C_ByVac.h>
#include <LiquidCrystal_SI2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR1W.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <SI2CIO.h>
#include <SoftI2CMaster.h>
LiquidCrystal lcd(A4,A5,A3,10,11,12); //Define LCD pins
// Define all the LED and Button pins.
// {RED, GREEN, YELLOW, BLUE}
int buttons[] = {3, 5, 7, 9}; //The four button input pins
int leds[] = {2, 4, 6, 8}; // LED pins
int sequence[100];
int largestIndex = 0;
int buttonPin = 2;
const int START = 0;
const int PLAY = 1;
const int GAMEOVER = 2;
int gameState;
int speed = 500;
boolean running = true; // Flag to know is knightrider is running
long currentMillis = 0;
long previousMillis = 0;
// the setup routine runs once when you press reset:
void setup() {
for(int pin=0; pin<4; pin++)
{
pinMode(leds[pin], OUTPUT);
}
for(int pin=0; pin<4; pin++)
{
pinMode(buttons[pin], INPUT);
}
Serial.begin(9600);
gameState = START; // Start the game with the start.
randomSeed(analogRead(40)); // initializes the pseudo-random number generator,
}
// the loop routine runs over and over again forever:
void loop()
{
lcd.begin(16, 2); // size of LCD
lcd.clear(); // clear previous
lcd.print("Press any button to start");
if(gameState == START)
{
waitToStart();
}
else if(gameState == PLAY)
{
Serial.println("Start");
showSequence();
readSequence();
}
else if(gameState == GAMEOVER)
{
Serial.println("Gameover");
blinkAll(5);
}
gameState = START;
}
void showSequence()
{
//blinkRed(2);
sequence[largestIndex] = random(0,4);
Serial.println("Next out");
Serial.println(sequence[largestIndex]);
largestIndex++;
for(int index=0; index<largestIndex; index++)
{
delay(500); // *changed from 300 to 500
digitalWrite(leds[sequence[index]], HIGH);
delay(700); // how long the LED's light for during game
digitalWrite(leds[sequence[index]], LOW);
}
//blinkGreen(2);
}
void readSequence()
{
//blinkYellow(2);
int positionPressed;
boolean madeMistake = false;
for(int index=0; index<largestIndex & madeMistake == false; index++)
{
Serial.println("");
Serial.println("Should push");
Serial.println(sequence[index]);
positionPressed = waitForButton(5000); // 0, 1, 2, or 3 - time for it
Serial.println("Pressed");
Serial.println(positionPressed);
if(positionPressed == -1 | positionPressed != sequence[index])
{
madeMistake = true; // Exit the loop.
gameState = GAMEOVER;
}
}
//blinkBlue(2);
}
// Returns the position of Button pressed (0, 1, 2, or 3) or -1 if no button is pressed in the time period.
int waitForButton(int delay)
{
int buttonPressed = -1;
int input;
boolean buttonBackUp = false;
currentMillis = millis(); // The number of ms since the program started running
previousMillis = currentMillis; // Records the point when we start spinning the loop.
// Keep spinning the loop until "delay" seconds have passed.
while (currentMillis - previousMillis < delay & buttonBackUp == false)
{
// Read the button and record when it has been pushed down.
for(int pin = 0; pin < 4 & buttonBackUp == false; pin++)
{
if(digitalRead(buttons[pin]) == HIGH)
{
buttonPressed = pin;
// Show the LED pushed.
digitalWrite(leds[pin], HIGH);
// It is possible the button is still being pushed.
// This loop spins until the button is let up.
while (currentMillis - previousMillis < delay & buttonBackUp == false)
{
input = digitalRead(buttons[pin]);
if(input == LOW)
{
buttonBackUp = true;
}
currentMillis = millis();
}
// Turn the LED pushed off.
digitalWrite(leds[pin], LOW);
// See if they took to long.
if(currentMillis - previousMillis > delay)
{
buttonPressed = -1; // They took to long to let the button up so they lose.
}
}
}
currentMillis = millis();
}
return buttonPressed;
}
void waitToStart()
{
int buttonPressed = -1;
allOff();
for(int pin = 0; pin < 4; pin++)
{
if(buttonPressed == -1)
{
digitalWrite(leds[pin], HIGH);
buttonPressed = waitForButton(800);
digitalWrite(leds[pin], LOW);
}
}
if(buttonPressed != -1)
{
// A button was pushed so wait then start playing.
delay(2000);
largestIndex = 0; // Restart
gameState = PLAY;
}
}
// Turns all the LEDs off.
void allOff()
{
for(int pin = 0; pin < 4; pin++)
{
digitalWrite(leds[pin], LOW);
}
}
// Turns all the LEDs on.
void allOn()
{
for(int pin = 0; pin < 4; pin++)
{
digitalWrite(leds[pin], HIGH);
}
}
// Spins a loop until "delay" milliseconds passes.
// While the loop is spinning we keep looking at the value of the button to see if pushed.
boolean readAnyButton(int delay)
{
boolean buttonDown = false;
currentMillis = millis(); // The number of ms since the program started running
previousMillis = currentMillis; // Records the point when we start spinning the loop.
// Keep spinning the loop until "delay" seconds have passed.
while (currentMillis - previousMillis < delay & buttonDown == false)
{
// Read the button and record when it has been pushed down.
for(int pin = 0; pin < 4; pin++)
{
if(digitalRead(buttons[pin]) == HIGH)
{
buttonDown = true;
}
}
currentMillis = millis();
}
return buttonDown;
}
void blinkAll(int times)
{
for(int count = 0; count < times; count++)
{
allOn();
delay(300);
allOff();
delay(300);
}
}
void blinkRed(int times)
{
for(int count = 0; count < times; count++)
{
digitalWrite(leds[0], HIGH);
delay(300);
digitalWrite(leds[0], LOW);
delay(300);
}
}
void blinkGreen(int times)
{
for(int count = 0; count < times; count++)
{
digitalWrite(leds[1], HIGH);
delay(300);
digitalWrite(leds[1], LOW);
delay(300);
}
}
void blinkYellow(int times)
{
for(int count = 0; count < times; count++)
{
digitalWrite(leds[2], HIGH);
delay(300);
digitalWrite(leds[2], LOW);
delay(300);
}
}
void blinkBlue(int times)
{
for(int count = 0; count < times; count++)
{
digitalWrite(leds[3], HIGH);
delay(300);
digitalWrite(leds[3], LOW);
delay(300);
}
}