0

Alright, so this code is for a game that is a power source connected to 6 buttons at ports 1, 2, 9, 11, 12, and 13. These are consequently supposed to light up 6 LEDs, each connected to 1. If you press the buttons in the right order, all the LEDs blink at once. Does it work?

#define DEBOUNCE 10  // button debouncer, how many ms to debounce, 5+ ms is usually plenty

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {1, 2, 9, 11, 12, 13}; // the analog 0-5 pins are also known as 14-19
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'currently pressed' 
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
byte i;

// set up serial port
Serial.begin(9600);
Serial.print("Button checker with ");
Serial.print(NUMBUTTONS, DEC);
Serial.println(" buttons");

// pin1 LED
pinMode(1, OUTPUT);

 // Make input & enable pull-up resistors on switch pins
 for (i=0; ipinMode(buttons[i], INPUT);
 digitalWrite(buttons[i], HIGH);
  }

 // Run timer2 interrupt every 15 ms 
 TCCR2A = 0;
 TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

 //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;

 }

SIGNAL(TIMER2_OVF_vect) {
check_switches();
}

void check_switches()  
{
  static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
 static long lasttime;
 byte index;

  if (millis() // we wrapped around, lets just try again
     lasttime = millis();
  }

  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
return; 
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();

  for (index = 0; index digitalRead(buttons[index]);   // read the button

    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */

    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH))        {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH     means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally  of the buttons
  }
}


void loop() {
  for (byte i = 0; i if (justpressed[i]) {
      justpressed[i] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just pressed"); 
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
    if (justreleased[i]) {
      justreleased[i] = 0;
      Serial.print(i, DEC);
      Serial.println(" Just released");
      // remember, check_switches() will CLEAR the 'just pressed' flag
    }
    if (pressed[i]) {
      Serial.print(i, DEC);
      Serial.println(" pressed");
      // is the button pressed down at this moment
    }
  }
}

int questionNumberArray[] = {1, 2, 9, 11, 12, 13};//the array itself
const size_t n = sizeof(questionNumberArray) / //the array used
sizeof(questionNumberArray[0]);//the base of the array

for (size_t i = 0; i < n - 1; i++) //the loop itself
{
size_t j = random(1, n - i);
int t = questionNumberArray[i]; //integer output for increase
questionNumberArray[i] = questionNumberArray[j];//the value definition
questionNumberArray[j] = t;
pinMode(questionNumberArray, INPUT);
if (digitalRead(questionNumberArray) == HIGH) {
  then(digitalRead(1, 2, 9, 11, 12, 13 == High(3))
  break;  
}
}

Also, how do I make it so that people have infinite times to try

2

1 Answer 1

1

There is very little about the code you are producing at the moment that could be classified as "right".

You are like a child who has been playing with LEGO® being let loose on the workings of a nuclear reactor. You're sticking things together without a) knowing what they do and b) knowing how they are supposed to work, and it's all going to end in tears.

I suggest that, for the time being, you forget this game project of yours and go back to the basics. Start by learning how to make an LED light up when you press a button. That will teach you both how to read the state of a button and how to control an LED. Then move on to doing it with multiple LEDs and multiple buttons.

As it stands you are just going to end up getting frustrated and annoyed. You're asking questions without either knowing just what it is you are asking or how to understand the answers when you are given them. And that is going to end up reflecting badly on the rating of your various users (hint: if you ask reasonable questions you get reasonable answers, and that means upvotes and reputation, which then means you can ask more questions so you don't have to keep creating new users to ask more questions where you don't actually understand what you're asking).

14
  • If only this "game project" wasn't an assigment due to be handed in tomorrow. Commented Oct 19, 2015 at 17:35
  • @CharlieHanson Sometimes you have to be cruel to be kind. If someone just jumps in at the end of a project at the last minute without doing any of the preliminary research then they're doomed to fail. After a few repeats they may finally learn. When you do a project on a piece of equipment or in an environment you don't know the first thing to do is learn how to use it. Only then can you even begin to consider doing anything more complex than blink an LED or print "Hello World".
    – Majenko
    Commented Oct 19, 2015 at 17:40
  • I'm not disputing that, I was simply adding that this project has been mandated rather than willingly chosen. Commented Oct 19, 2015 at 17:43
  • 1
    I agree with Majenko. Forget this game and learn how to code in C. If you don't know, and want us to write your code for you, then you will be fraudulently passing this test. Even if you don't care about that, someone will, one day. Imagine your teacher asks you a simple question about the code: "why did you do X?". Your complete inability to explain the code, or modify it, will expose that someone else wrote it.
    – Nick Gammon
    Commented Oct 19, 2015 at 20:09
  • 1
    how is telling me I missed 6 lectures useful ... He said cheating is fine - how is cheating useful? You pass a course by cheating? Does that mean you then design an aircraft without knowing what you are doing because you cheated your way through the course? Please supply your teacher's name so we can write to his supervisor. That teacher should be sacked. If you are telling the truth, that is. Which I doubt.
    – Nick Gammon
    Commented Oct 20, 2015 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.