Go Down

Topic: desired result not being achieved with button pad (Read 959 times) previous topic - next topic

big93

Mar 29, 2008, 05:19 pm Last Edit: Mar 29, 2008, 05:21 pm by big93 Reason: 1
hi!

i'm tryig to make my button pad work, anmd i'm working on the actual buttons now, so from my code, you will notice i turn pins 0-3 on and off, one by one so i can scan which row of and column button is being pressed, but unfortunitly when i debug by printing in the value, it only half works, and half the butto pad is not even acknowledged...

so here is my code, if u need help understanding what i did and why, please ask, becuase this is the finale step to get this code working :)

::

Code: [Select]
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK  13//sck
#define SLAVESELECT 10
byte potpin = 0;
byte r1=0;  //sets the correct pins to the right color, do not change!
byte g2=1;
byte g1=2;
byte b2=3;
byte b1=4;
byte r2=5;
byte delay1 = 100; // my delay value
byte val1 = 0;  // the value i hope to fade
byte direction = 0;
char spi_transfer(volatile char data)
{
 SPDR = data;                    // Start the transmission
 while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
 {
 };
 return SPDR;                    // return the received byte
}

void setup()
{
 byte i;
 byte clr;
 pinMode(DATAOUT, OUTPUT);
 pinMode(DATAIN, INPUT);
 pinMode(SPICLOCK,OUTPUT);
 pinMode(SLAVESELECT,OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(14, OUTPUT);
 pinMode(15, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(2, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(16, INPUT);
 pinMode(17, INPUT);
 pinMode(18, INPUT);
 pinMode(19, INPUT);
 digitalWrite(SLAVESELECT,HIGH); //disable device
 // SPCR = 01010000
 //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
 //sample on leading edge of clk,system clock/4 (fastest)
 SPCR = (1<<SPE)|(1<<MSTR);
 clr=SPSR;
 clr=SPDR;
 delay(10);
 for (i=0;i<6;i++)
 {
   write_pot(i,255);
 }
}

byte write_pot(int address, int value)
{
 digitalWrite(SLAVESELECT,LOW);
 //2 byte opcode
 spi_transfer(address);
 spi_transfer(value);
 digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer

Serial.begin(9600);}
void loop()
{
Serial.println(digitalRead(18));

 digitalWrite(15, LOW);
 digitalWrite(14, HIGH);
delay(delay1);
 digitalWrite(7, LOW);                    //button 1, 1  and 1, 2:
//if( digitalRead(1) = HIGH && digitalRead(16) == HIGH )
//{ write_pot(r1, 0);}
//else
//{write_pot(r1, 255);}
delay(delay1);
digitalWrite(0, HIGH);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);                // button 2, 1 and 2, 2:
//write_pot(r1, 255);
delay(delay1);
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);                // button 3, 1 and 3, 2:

delay(delay1);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);                  //button 4, 1 and 4, 2:

delay(delay1);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);

digitalWrite(15, HIGH);
digitalWrite(14, LOW);
   
                    // second relay starts here
          delay(delay1);              
digitalWrite(3, LOW);                
digitalWrite(7, LOW);            // button 1, 3 and 1, 4:


 delay(delay1);
digitalWrite(7, HIGH);
digitalWrite(6, LOW);               //button 2, 3 and 2, 4:

delay(delay1);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);             //button 3, 3 and 3, 4:

delay(delay1);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);              // button 4, 3 and 4, 4:

delay(delay1);
digitalWrite(4, HIGH);




}

simond

You could start by removing the Serial.begin() from write_pot and moving it to setup(). That will help the debugging. Next step is to explain what you mean by 'half working'.

big93

oh... i thought it was in setup already, ill move dat now.

but my main problem is that i don't undertsand how to make this work...

heres is my understanding:
i am sapposed to scan through the buttons, like row 1, on row, then row 1 off, row 2 on, then keep going in an infinite circle... and then i check the columns for presses. right?

but when i hooked it up, and held down the buttons, i never recieved "0 0 0 1 0 0 0 1" i always either got  "1 1 1 1 1 1" or a random values like "0 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 0 0 0 "

is that becuase the if statement has to be in a certain place? and inside the if statement, i have to check for both the output pin, and the input pin to be on for the buttons to be true? right? i'm just so confused, i might just have to make a new sketch just for the buttons...

simond

I don't think your code is doing what you say you want. I don't really see any scanning over rows or columns in there, for one, just a bunch of digitalWrite()s.

A good general debugging technique is to break down the code into as small a set of instructions as are necessary to replicate the desired functionality, as possible. IOW, make a sketch that does nothing but scan over a single row, checking for each button, and printing the result over serial. Once you have this working, you can expand it to cover all rows.

big93

when i mean scanning, i mean that each row is turned on, and then a column is checked for a press, there isint any checking until i write an if statement, but when you see me write digitalWrite(0, HIGH) for pins 0 - 3 ( now changed to pins 8,9,3,4 becuase of problems ) that's what's going on, i'm sending each row a 5v, and then if i wanted to check for a button press, i thought i could make an if staement anywhere saying if pin 2 ( the output 5v button pin ) and pin 16 ( an incoming button pin ) both equal true, then a certain button is being pressed...

this whole row, column thing is a good idea from a looking point, but from a building pint, this just seems so stupid and unecessary... ( even though, when i get this wroking, i would have saved myself 8 pins...

so i gotta make this work, cuz theres no other way unfortunitly

Triffid Hunter

do you have resistors pulling the columns low when nothing is being pressed? Without that, the inputs will simply go high and stay there as soon as you press a button, and their initial state will be subject to random fluctuations in the electric fields around the project.

big93

yeppers, i don't have the exact values on hand, but i have, so here are the colors:

brown black orange resistor connected to ground
connected to the brown black orange is both the button output, and a brown black brown resistor, which is connected to the pin.

Triffid Hunter

Quote
yeppers, i don't have the exact values on hand, but i have, so here are the colors:

brown black orange resistor connected to ground
connected to the brown black orange is both the button output, and a brown black brown resistor, which is connected to the pin.


10k to ground and 100r to input, sounds good. input resistor isn't really needed but shouldn't hurt either.

btw, how much time are you leaving between setting the outputs and reading the inputs? keypads and their connecting wires have enough capacitance that if you do one then immediately the other, you may get bogus readings. a couple dozen microseconds should do it, maybe count to 32 or something.

big93

thats the problem, in my code, the whole code is running at at about 12 ms at the most, and i can't make a seprate timer for the buttons ( or can i and i just don't know about it? )

cuz i change things very fast, if u see i made it 1 ms ( or it may look like 100 cuz i was debugin at only 100 ms...)

i might have to use that dual core arduino from the exhibition to make this simpler, but then again, ill keep trying till theres no more trying...


Triffid Hunter

Whenever I've needed to do delays in this sort of loop, I simply wrote the next value out just after reading the columns rather than just before.

big93

i'm not sure what your getting at...

do you mean just make a section for their own delays in the code?

Go Up