Arduino memory game - arduino

This game has 4 leds and 4 buttons. the game is turning RANDOMLY those leds ON and OFF.
The player should be able to push the right button whenever he sees one led to be turned ON.
The leds should be turning ON and OFF with incerasing speed, so reaction time of a player is shorter and shorter
I have this code but i just know how to add more leds and buttons.
const int BUTTON1 = A0;
const int BUTTON2 = A1;
const int BUTTON3 = A2;
const int BUTTON4 = A3;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int ran;
int right = 0;
int ledOrder[9];
int guessOrder[9];
void setup()
{
Serial.begin(9600);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(BUTTON1,INPUT);
pinMode(BUTTON2,INPUT);
pinMode(BUTTON3,INPUT);
pinMode(BUTTON4,INPUT);
}
void randomLed() {
for (int i = 0; i < 9; i++) {
ran = random(1,20);
if (ran < 11) {
digitalWrite(LED1,HIGH);
delay(500);
digitalWrite(LED1,LOW);
ledOrder[i] = 1;
}
else {
digitalWrite(LED2,HIGH);
delay(500);
digitalWrite(LED2,LOW);
ledOrder[i] = 2;
}
delay(500);
}
}
void btnClick() {
int ans = 0;
while (ans < 9) {
if (digitalRead(BUTTON1) == HIGH) {
guessOrder[ans] = 1;
ans++;
while (digitalRead(BUTTON1) == HIGH) {
}
}
else if (digitalRead(BUTTON2) == HIGH) {
guessOrder[ans] = 2;
ans++;
while (digitalRead(BUTTON2) == HIGH) {
}
}
}
}
void loop() {
Serial.print("Press button1 to start \n");
while (digitalRead(BUTTON1) == LOW) {
}
randomLed();
btnClick();
for (int i = 0; i < 9; i = i + 1) {
Serial.print("Guess: ");
Serial.print(guessOrder[i]);
Serial.print(" Answer: ");
Serial.print(ledOrder[i]);
if (guessOrder[i] == ledOrder[i]) {
Serial.print(" Right");
right++;
} else {
Serial.print(" Wrong");
}
Serial.print("\n ");
}
Serial.print(right);
Serial.print("/9\n");
delay(2000);
}

If I understand your question correctly, you would like to increase the led blink rate(when the LED's come on) each time the user gets the previous game right. If this is true you should try to put loop in for/if the user got the the previous one(game) right it will increase blink rate by a simple increment( I++;). Comment if you feel this is clarified enough or if your asking for something else

Related

why my audio sound is not playing in my arduino code but if i play it separately it started working good

I am trying to set the timer after which the audio will automatically played once but it is not playing anything just a noise but when i run the audio program separately it work perfectly good
This is my code please help me out where i am doing wrong.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
int timer1_counter;
#define SD_ChipSelectPin 4
TMRpcm tmrpcm;
unsigned long time_now = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ledPin = 13;
const byte interruptPin1 = 2;
const byte interruptPin2 = 3;
int counter2 = 0;
volatile byte state = LOW;
int count = 0;
int limit = 0;
bool TimerFlag = false;
int deviceTime = 0;
int set = 5;
bool soundplayflag = false;
void setup()
{
lcd.begin();
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(interruptPin1, INPUT_PULLUP);
pinMode(interruptPin2, INPUT_PULLUP);
pinMode(set, INPUT_PULLUP);
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Please Select:");
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
// initialize timer1
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
if (soundplayflag == true)
{
counter2 = counter2+1;
Serial.println(counter2/2);
}
}
void loop()
{
//unsigned long currentMillis = millis();
int up = digitalRead(interruptPin1);
int down = digitalRead(interruptPin2);
int setbutton = digitalRead(set);
delay(230);
if (up == 0)
{
Serial.println("Entering up");
count++;
if (count >= 0) {
lcdprint(count);
}
}
else if (down == 0)
{
Serial.println("Entering down");
count--;
if (count >= 0) {
lcdprint(count);
}
}
else if (setbutton == 0)
{
Serial.println("Entering set");
soundplayflag=true;
deviceTime = count;
}
if (deviceTime > 0)
{
if (deviceTime == counter2)
{
soundplayflag=false;
Serial.println("Hello world");
PlaySound();
counter2 = 0;
}
}
}
int lcdprint(int a)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Please Select:");
lcd.setCursor (7,1);
lcd.print(a);
Serial.println(a);
lcd.setCursor (10,1);
lcd.print("Min");
}// end of lcdprint(int a)
void PlaySound()
{
for (int i = 0; i < 10; i++)
{
tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
delay(1000);
}// end of for loop
}// end of void PlaySound()
my expected output is it should play the sound when i set the time
It seems TMRpcm uses Timer1, which conflicts with your TIMER1_COMPA_vect. That would explain why it's working if you run nothing else.
Maybe try to use #define USE_TIMER2?

Arduino Combination Button?

I am trying to set up an Arduino Uno plus a RelayShield along with 6 push buttons(standard), and 3 LED's(red, yellow, green). Now what I have code to do is to take in a button push combination that is 6 pushes long(order only matters for what is set in the array). I have everything wired up to a breadboard and all that other jazz. My problem is, none of the LED's are working(I followed detailed instructions from instructables - minus the code), and it doesn't matter how many button pushes there are, only button 3 triggers the relay.... Now, for simplicities sake, I didn't wire up all 6 buttons, and I just shortened the array down to 3 and adjusted accordingly(I didn't want to hook up 6 buttons right now). Can someone verify this code for me? Or tell me what is wrong with it? Thanks in advance!
//button pins
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
const int button6 = 7;
const int grnLed = 9;
const int redLed = 10;
const int yellowLed = 11;
const int openRelay = 8;
// how long is our code, kinda personal
const int codelen = 3;
//pin code values must match button pin values
char PIN[codelen] = {
'1', '2', '3'
};
// attempted combo
char attempt[codelen] = {
'0', '0', '0'
};
// attempt count
int z = 0;
void setup() {
// you've been setup
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
pinMode(openRelay, OUTPUT);
// set pullup resistor for buttons
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
// set openRelay state to open or closed
digitalWrite(openRelay, LOW);
}
void correctPIN()
{
pulseLED(grnLed, 3000);
digitalWrite(openRelay, HIGH);
delay(2000);
digitalWrite(openRelay, LOW);
delay(2000);
z = 0;
}
void incorrectPIN()
{
pulseLED(redLed, 3000);
z = 0;
}
void checkPIN()
{
int correct = 0;
int i;
for (i = 0; i < codelen; i++)
{
if (attempt[i] == PIN[i])
{
correct++;
}
}
if (correct == codelen)
{
correctPIN();
}
else
{
incorrectPIN();
}
for (int zz = 0; zz < codelen; zz++)
{
attempt[zz] = '0';
}
}
void checkButton(int button){
if (digitalRead(button) == LOW)
{
while (digitalRead(button) == LOW) { } // do nothing
//convert int to string for tracking/compare
char buttStr = button + '0';
attempt[z] = buttStr;
z++;
//light up led so we know btn press worked
pulseLED(yellowLed, 500);
}
}
void pulseLED(int ledpin, int msec) {
digitalWrite(ledpin, HIGH);
delay(msec);
digitalWrite(ledpin, LOW);
}
void loop() {
// check buttons
checkButton(button1);
checkButton(button2);
checkButton(button3);
checkButton(button4);
checkButton(button5);
checkButton(button6);
//if number of buttons pressed, z, matches code/pin length then check
if (z >= codelen)
{
checkPIN();
}
}

invalid conversion from 'int*' to 'uint8_t

im having a problem, i just bought an arduino and i was wondering if anyone could help, here is my code. (i am just trying to get two leds to fade in and out).
int ledCount = 2;
int ledPins [ ] = {11,12 };
int brightness = 0;
int delayTime = 10;
void setup() {
pinMode(ledPins, OUTPUT);
}
void loop() {
while(brightness < 255)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness + 1;
}
while(brightness > 0)
{
analogWrite(ledPins, brightness);
delay(delayTime);
brightness = brightness - 1;
}
}
You are passing an array to analogWrite or pinMode, where it is expecting a uint8_t.
Arduino pin manipulation functions will only handle a single pin at a time. There are ways around that, by directly manipulating the AVR/ARM GPIO registers, but those can be finicky (not recommended for use unless you really need speed).
The reason it says int * is because under the hood, arrays in C/C++ are represented as pointers.
If you want to analogWrite or pinMode to both LEDs, you will have to call the function once for each LED. Example:
analogWrite(ledPins[0], brightness);
analogWrite(ledPins[1], brightness);
Or
for(int currentLED = 0;currentLED < ledCount;i++){
analogWrite(ledPins[currentLED], brightness);
}
In the context of your program:
int ledCount = 2;
int ledPins [] = {11, 12};
int brightness = 0;
int delayTime = 10;
#define INCREASE 1
#define DECREASE 2
int brightness_change = INCREASE;
void setup(){
for(int i = 0;i < ledCount;i++){
pinMode(i, OUTPUT);
}
}
void loop(){
while(brightness < 255 && brightness_change == INCREASE){
brightness = brightness + 1;
}
while(brightness > 0 && brightness_change == DECREASE){
brightness = brightness - 1;
}
if(brightness == 255){
brightness_change = DECREASE;
}
if(brightness == 0){
brightness_change = INCREASE;
}
for(int current_led = 0;current_led < ledCount;current_led++){
analogWrite(current_led, brightness;
}
delay(delayTime;
}
Not tested, but it should work.

Why doesn't my servo control work?

Basically I'm trying to control a servo with two push buttons (one for forward and one for backward). However, my code doesn't work and I am not sure why. Essentially I used the Sweep and Button examples to make this code. However, it doesn't seem to be working unless something is wrong with my hookup.
#include <Servo.h>
Servo servoOne;
int servoOnePos = 0;
const int buttonUpPin = 13;
const int buttonDownPin = 12;
int buttonUpState = 0;
int buttonDownState = 0;
void setup() {
servoOne.attach(11);
pinMode(buttonUpPin, INPUT);
pinMode(buttonDownPin, INPUT);
}
void loop() {
buttonUpState = digitalRead(buttonUpPin);
buttonDownState = digitalRead(buttonDownPin);
if (buttonUpState == HIGH) {
for (servoOnePos < 180; servoOnePos += 1;) {
servoOne.write(servoOnePos);
delay(15);
}
} else if (buttonDownState == HIGH) {
for (servoOnePos <= 180; servoOnePos = servoOnePos - 1;) {
servoOne.write(servoOnePos);
delay(15);
}
}
}
#include <Servo.h>
Servo servoOne;
int pos = 90;
const int maxDeg = 160;
const int minDeg = 5;
const int buttonUpPin = 13;
const int buttonDownPin = 12;
const int leftPin = 3;
const int rightPin = 2;
int leftPressed = 0;
int rightPressed = 0;
void setup()
{
servoOne.attach(11);
pinMode(buttonUpPin, INPUT);
pinMode(buttonDownPin, INPUT);
}
void loop()
{
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
if(leftPressed){
if(pos < maxDeg) {
pos += 3;
}
servoOne.write(pos);
}
if(rightPressed){
if(pos > minDeg) {
pos -= 3;
}
servoOne.write(pos);
}
delay(15);
}

Trying to avoid goto function in my Arduino circuit that attempts to create a "Smart Waiter"

Basically, I am making an Arduino Uno project that involves creating a smart waiter that:
Will have a glass holder on top of the basic Arduino chassis. The sensors will be:
4 Infrared Sensors, one on each side
2 Line Sensors, one left and one right
Light Sensor, one the cup holder
It will also have:
2 Servo Motors, one left and right
1 LED showing cup status
It will:
Move along a black line that will be taped on the floor of the HOP
Will sense people on all sides, and pause when it senses close bodies
Will stop indefinitely if it senses that the glass has been lifted
Will start again in 15 seconds if it doesn’t sense movement of glass OR will start again when glass is put down
After glass has been put down, LED will turn from green to red
Robot will continue to move, ignoring all obstacles, until it reaches filling station
Will stop at filling station, where filler will replace glass and press Arduino hard reset button
My Code is:
#include <Servo.h>
int redLED = 5;
int yellowLED = 6;
int greenLED = 7;
int cup1 = 0;
int picked = 1;
int lineright = 0;
int lineleft = 0;
int sensorright = 0;
int sensorleft = 0;
int sensorfront = 0;
int sensorback = 0;
int sensorpinright = 1;
int sensorpinleft = 2;
int sensorpinfront = 3;
int sensorpinback = 4;
Servo servoright;
Servo servoleft;
void setup ()
{
servoright.attach (9);
servoleft.attach (10);
pinMode (redLED, OUTPUT);
pinMode (greenLED, OUTPUT);
pinMode (greenLED, OUTPUT);
}
void loop()
{
sensorright = digitalRead (sensorpinright);
sensorleft = digitalRead (sensorpinleft);
sensorfront = digitalRead (sensorpinfront);
sensorback = digitalRead (sensorpinback);
lineleft = analogRead (1);
lineright = analogRead (2);
cup1 = analogRead (3);
if (sensorright < 0 && sensorleft < 0 && sensorfront < 0 && sensorback < 0)
{
digitalWrite (led1, GREEN);
startfresh:
if (lineleft < 800)
{
servoleft.write (180);
servoright.write (180);
}
else
{
if (lineright < 800)
{
servoright.write (0);
servoleft.write (0);
}
else
{
servoright.write (0);
servoleft.write (180);
}
}
}
else
{
for (int i=0;i<5;i++)
{
servoright.write (93);
servoleft.write (93);
if (picked < 1)
{
while (cup1<500)
{
digitalWrite (yellowLED, HIGH);
picked = 1;
}
}
else
{
digitalWrite (yellowLED, LOW);
digitalWrite (redLED, HIGH);
goto startfresh;
}
delay (2000)
}
}
}
What I want to do with my code is to avoid the goto function at the end. However, I cannot find any way to restructure this with the limitation of only 2 functions. In this case, the goto function seems fine, but I am not sure. Is there any simple way to restructure this?
Make startfresh as function
if (sensorright < 0 && sensorleft < 0 && sensorfront < 0 && sensorback < 0)
{
digitalWrite (led1, GREEN);
startfresh();
}
else
{
for (int i=0;i<5;i++)
{
servoright.write (93);
servoleft.write (93);
if (picked < 1)
{
while (cup1<500)
{
digitalWrite (yellowLED, HIGH);
picked = 1;
}
delay (2000);
}
else
{
digitalWrite (yellowLED, LOW);
digitalWrite (redLED, HIGH);
startfresh();
}
}
}
}
void startfresh() {
if (lineleft < 800)
{
servoleft.write (180);
servoright.write (180);
}
else
{
if (lineright < 800)
{
servoright.write (0);
servoleft.write (0);
}
else
{
servoright.write (0);
servoleft.write (180);
}
}
}

Resources