Digital input

Hi everyone,

I got a project on arduino but I am facing a problem. My idea is to use 12 push buttons on a arduino uno to send characters to a visual application and change de coulour of the shape when the coresponding button is pressed.

I got an example for one button and it’s work perfectly, but for the project i have to submit, i should use 12 buttons. I have tryed to expand the basic code whitch is working using switch but it doesn’t work.
:frowning: :frowning: please help me with a good ans easy solution.
here is the working arduino code

int druk=2;
void setup()
{
Serial.begin(9600);
pinMode(druk,INPUT);
}

void loop()
{
int statusdruk = digitalRead(druk);
if(statusdruk==HIGH)
{
Serial.print(‘a’);
}
else
{
Serial.print(‘b’);
delay(100);
}

This is the one i have tryed

int druk= 2;
int brok= 3;
void setup()
{
Serial.begin(9600);
pinMode(druk, INPUT);
pinMode(brok, INPUT);
}
void loop()
{
int state = HIGH;
int 1 = digitalRead(druk);
int 2 = digitalRead(brok);
switch(state)
{
case 1 :
Serial.print(“b”);
break;
case 2 :
Serial.print(“a”);
break;
default:
Serial.print(" ");
}
delay 10;
}

This is the visual basic code, this one is also working perfectly for only one button

Private Sub Command1_Click()
Form1.Hide
End Sub
Private Sub Form_Load()
MSComm1.RThreshold = 3
MSComm1.InputLen = 3
MSComm1.Settings = “9600,n,8,1”
MSComm1.CommPort = 4
MSComm1.PortOpen = True
MSComm1.DTREnable = False
End Sub
Private Sub MSComm1_onComm()
Dim drunk As String
If MSComm1.CommEvent = comEvReceive Then
sdata = MSComm1.Input
druk = Mid$(sdata, 1, 1)
Label1.Caption = druk
If druk = “1” Then
Text1.Text = “switch is open”
Shape1.FillColor = vbRed
Else
Text1.Text = “switch is close”
Shape1.FillColor = vbGreen
End If
End If
End Sub

int 2 = digitalRead(brok);That didn't compile, did it?
You can't have a variable name starting with a digit.

Please remember to use code tags when posting code.

For your switch/case, you gave the variable 'state' the value HIGH - that's never going to equal 2

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

arkatakor:
My idea is to use 12 push buttons on a arduino uno to send characters to a visual application

So basically you want to have 12 push buttons, each of them assigned a single character, and then have a function to read the push buttons as if it's a keyboard, correct?

I think I have something ready here, which works with two arrays defined: One array defining the pin numbers, the other array defining the characters.

data structure for seven push buttons assigned to plus-sign, minus-sign and lowercase letters 'a' to 'e':

const byte buttonPins[] = {2, 3, 4, 5, 6, 7, 8};
const char buttonChars[] = {'+', '-', 'a', 'b', 'c', 'd', 'e'};

Would you like to see more, like code for a function and example program how to read the character codes when a button is pressed?

My function would work like that:

  • a button changes its state from released to pressed ==> return assigned character code
  • a button is kept pressed all the time ==> return -1
  • a button is released and changing state from pressed to released == return -1
  • a button is kept released all the time ==>return -1

So you will get only one code per state change from freleased state to pressed state.
No "rpeat codes or something like that.
One time pressing the push button ==> one time getting the character code
Is it that what you want?
That's what I understood from your initial posting.

arkatakor:
Hi everyone,

I got a project on arduino but I am facing a problem. My idea is to use 12 push buttons on a arduino uno to send characters to a visual application and change de coulour of the shape when the coresponding button is pressed.

First, I would arrange the 12 buttons in a 3 by 4 matrix to save on input pins.

Here’s a simple keyboard matrix scan function you may be able to use:

// Arduino pins connected to COLUMNS
#define COL1 2
#define COL2 3
#define COL3 4
// Arduino pins connected to ROWS
#define ROW1 5
#define ROW2 6
#define ROW3 7
#define ROW4 8

// scan keyboard matrix. return unique key code
// or "-1" if more than one key was pressed.
int scan_matrix (void)
{
    uint8_t cc, rc, col, row, mult;
    int n;

    const uint8_t cols[] = {
        COL1, COL2, COL3,
    };

    const uint8_t rows[] = {
        ROW1, ROW2, ROW3, ROW4,
    };

    cc = (sizeof (cols) / sizeof (*cols)); // cc == "column count"
    rc = (sizeof (rows) / sizeof (*rows)); // rc == "row count"

    mult = (cc < rc) ? rc : cc; // multiplier the larger of the two

    for (n = 0; n < cc; n++) {
        pinMode (cols[n], OUTPUT); // set columns as outputs
    }

    for (n = 0; n < rc; n++) {
        pinMode (rows[n], INPUT_PULLUP); // set rows as inputs
    }

    n = 0; // clear key value

    for (col = 0; col < cc; col++) {
        digitalWrite (cols[col], LOW); // bring a column low

        for (row = 0; row < rc; row++) {
            if (digitalRead (rows[row]) == LOW) { // if this row is low
                if (n == 0) { // if no key was recorded yet
                    n = ((col * mult) + row); // record this crosspoint
                } else { // more than one key == error
                    n = -1;
                }
            }
        }

        digitalWrite (cols[col], HIGH); // bring column back up
    }

    return n;
}

(deleted)

spycatcher2k:
And please remember to tell your teacher that you did not do any of the work, you got other to do it for you.

Hehe, that is so true. It feels like homework help desk here a lot of the time. It's amazing how many people try to convince someone to do their homework/assignment for them. I love those posts that say, "yeah but I still don't quite understand, could you just write the code for me." :stuck_out_tongue:

Krupski:
First, I would arrange the 12 buttons in a 3 by 4 matrix to save on input pins.

Here's a simple keyboard matrix scan function you may be able to use:

If you're using that code in any of your projects then may I suggest a slight modification to make it safer.

The problem with it is that, with all the columns configured as outputs, if a user accidentally pushes two buttons in the same row then it shorts a high and low output together. Briefly this might not damage anything, but it's obviously something that's best to avoid.

The workaround is just to leave all columns as inputs (hi-Z) until you need to scan, and then only set as an output the one line that you are driving low. :slight_smile: