This code is not working.. i need help

I really need help with this code. it compile without error but i upload it, it just wont work.
what am i doing wrong?

#include <Keypad.h>
#include <FastLED.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int keyindex;
const int arrsize = 12;
int i;

char sample[arrsize] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '#'};
char potnumber[arrsize] = {'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z'};
bool potplayed = false;
bool score = false;

#define NUM_LEDS 12
#define DATA_PIN 10    // we will figure this out during connection
CRGB leds[NUM_LEDS];

int tiltSensor = 11; // we will figure this out during connection
int tiltLed = 9;   // we will figure this out during connection
int vibeSensor = 15;  // we will figure this out during connection
int proxySensor = 16; // we will figure this out during connectio


void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
  pinMode(tiltSensor, INPUT);
  pinMode(vibeSensor, INPUT);
  pinMode(proxySensor, INPUT);
  pinMode(tiltLed, OUTPUT);

}

void loop() {

    char key = keypad.getKey();
    Serial.print(key);
   if(key != NO_KEY){
    for(i=0; i< arrsize; i++){
      if (key == sample[i]){
        i = keyindex;
        keyindex = keyindex;
        Serial.print( "   and the index is  " );
        Serial.print(keyindex);
      }
        if(key == potnumber[keyindex]){
          potplayed = true;
          Serial.println("Pot is used, try another pot");
              leds[keyindex] = CRGB::Red;
              FastLED.show();
              // do other stuff like buzzer or turn Pot LED red
              //delay(5000);
              
              leds[keyindex] = CRGB::Black;
              FastLED.show();
       
          
        }else 
        if(potplayed = false){
              potnumber[keyindex] = key;
              // do other stuff like led light
             if (digitalRead(tiltSensor) == HIGH) {
              digitalWrite(tiltLed, HIGH);
              
              leds[keyindex] = CRGB::Blue;
              FastLED.show();
              //delay(5000);

          
              }else if(digitalRead(tiltSensor) == LOW){
                digitalWrite(tiltLed, LOW);
                leds[keyindex] = CRGB::Black;
                FastLED.show();
              }
                
                
              if((digitalRead(tiltSensor) == HIGH) && (digitalRead(vibeSensor) == HIGH)){
                if (digitalRead (proxySensor == HIGH)){
                  score = true;
                  leds[keyindex] = CRGB::Green;
                  FastLED.show();
                  
                }
                
              }

              if((digitalRead(tiltSensor) == LOW) && (digitalRead(vibeSensor) == HIGH)){
                if (digitalRead (proxySensor == HIGH)){
                  score = true;
                  leds[keyindex] = CRGB::Green;
                  FastLED.show();
                  
                }
                
              }
              if((digitalRead(tiltSensor) == LOW) && (digitalRead(vibeSensor) == HIGH)){
                if (digitalRead (proxySensor == LOW)){
                  score = false;
                  leds[keyindex] = CRGB::Black;
                  FastLED.show();
                  
                }
                
              }
               

              
            
            
            }
          
          
          }
      
      




   }
  
}

What does that mean?

  • What were you expecting it to do?
  • What is it actually doing?
  • What investigation/debugging have you done to find the problem(s)?

The code doesn't live in isolation: it mentions things like tilt, vibration, and proxy sensors; a keypad; etc - we'd also need full details of those (post links) and how you have them connected (post your schematic).

What Arduino is it?

The code you provided is for a keypad and LED matrix game. The game uses a 4x3 keypad to select a number. The player then has to tilt the device to light up the corresponding LED. If the player can do this while the vibration sensor is also activated, they score a point. The game uses a proxy sensor to detect if the player is close enough to the device.

does this paint enough picture for you?

what I expected was to this....

when i press a key on the keypad, the key value from the keypad would be store in the same position as the first declare array. Two array exists, one has the values of the keypad the other does not. when the key is pressed the value is compare and store in the same position as in the first array, if the key is repeated within the cause of one session, the red light would come and serial monitor will show message. Right now i cant get pass the keypad.

When I push the key, only the first value would work. It as if the whole this freezes.

I used an arduino Mega

So forget about all the rest, and just concentrate on the keypad start.

The key to success in larger projects like this is to build step-by-step - get each part working separately before trying to bring them all together!

alright. but i did test only the keypad, it was fine. when i added the rest of the code the more like jams

1 Like

Hello jave005

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code as diagnostic prints to see the values of variables and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

already tried that. it was stuck at the second section in the loop. below the get key. that for loop is what got me scratching my head. it just keeps print when i press any other key apart from the first key.

So go back to just the keboard.

Add other stuff one bit at a time.

When it breaks, you know where to look ...

2 Likes

Please explain to me what you want with this part of your code?

 for(i=0; i< arrsize; i++){
      if (key == sample[i]){
        i = keyindex;
        keyindex = keyindex;
        Serial.print( "   and the index is  " );
        Serial.print(keyindex);
      }

i = keyindex; ???
keyindex = keyindex; ???

And this:

if (key == potnumber[keyindex])

(char potnumber[arrsize] = {'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z'}; ?????

Key will never be equal to 'z'.

This is not a compare, this means iqual:

if (potplayed = false) {

Correct form: if (potplayed == false) { // Double iqual

1 Like

To clarify, the single = is the assignment operation; so, in the above code, the value false is being assigned to the variable potplayed.
Therefore, the value of the if condition above will always be false.

The double == is the comparison operator "is equal to"

This is a very common mistake in C/C++; so much so that it's common practice to always write "is equal to" comparisons with the constant value on the left; eg,

if ( false == potplayed ) {

Then, if you accidentally write an = instead of ==:

if ( false = potplayed ) {

You will get an error.

Of course, when you're testing a bool variable, you don't need to include the comparison to true or false at all - just write:

if ( !potplayed ) {
   // potplayed is false
   :
   :

or

if ( potplayed ) {
   // potplayed is true
   :
   :

code is working now. I fixed the way you suggested. Adding one part after the other.

Thank you

Yes. It does.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.