#Arduino Help

1 messages ยท Page 7 of 1

wary cloak
#

I don't get the delay at all

#

So is it so

#

My fingers gets time to

#

Not be there any more

#

Or smth

coral geyser
wary cloak
#

Since all methods have this now, shouldn't get key also?

coral geyser
#

because when you press a mechanical switch the waveform looks like

wary cloak
#
pub fn get_key(&mut self) -> Option<char> {
        for col in 0..self.columns.len() {
            for row in 0..self.rows.len() {
                if self.check_row_col(row, col) == true {
                    return Some(self.matrix[row][col]);
                }
            }
        }
        None
    }
coral geyser
coral geyser
wary cloak
coral geyser
# wary cloak How?
for index in 0..buffer.len() {
  match self.wait_for_Key() {
    'a' => { return index; }
    key => {
      // put key in the buffer
      buffer[index] = key;
    }
  }
}
buffer.len()
wary cloak
#

Because we get rid of all the asking if there's nothing pressed?

coral geyser
wary cloak
#

And with the for loop I guess we no longer need the if and so..

#

Ok done

#

So know we finally write code to main?

coral geyser
#

yep

#

I think we have all the functionality you need right?

wary cloak
#

Yes unless you think of another cool stuff we could do or need but I don't think so

#

main is like this now

#
#![no_std]
#![no_main]

mod Keypad;

use panic_halt as _;


#[arduino_hal::entry]
fn main() -> ! {
    let dp = arduino_hal::Peripherals::take().unwrap();
    let pins = arduino_hal::pins!(dp);

    let serial = arduino_hal::Usart::new(
        dp.USART0,
        pins.d0,
        pins.d1.into_output(),
        arduino_hal::usart::Baudrate::new(57600),
    );

    loop {
        
    }
}
wary cloak
#

I came back from dinner

#

How tasty

#

So we should test the three methods and it would be done

#

Any ideas?

#

I guess

#

First I would need to make a variable that stores the matrix

#

But about the pins

coral geyser
#

in the loop wait for a string then when you get one print it to the serial?

wary cloak
#

First let me check for the pins.

#

Which ones were

coral geyser
# wary cloak How do I do it?
let rows = [
  pins.xx.into_pull_up_input().downgrade(),
  pins.xx.into_pull_up_input().downgrade(),
  pins.xx.into_pull_up_input().downgrade(),
  pins.xx.into_pull_up_input().downgrade(),
];
wary cloak
#
const char keys[FILAS][COLUMNAS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

const byte pinColumnas[COLUMNAS] = {22, 23, 24, 25};
const byte pinFilas[FILAS] = {26, 27, 28, 29};
#

Here is the code from the Arduino

wary cloak
#

Can I copy the declaration of the matrix as is? (Changing filas and columnas for 4)

coral geyser
wary cloak
coral geyser
wary cloak
#

Do I make it constant also?

#

The matrix

coral geyser
wary cloak
#

What's wrong?

coral geyser
#

and the ; should be ,

wary cloak
#

At the end or separating the arrays?

coral geyser
#
let keys = [
  [ '1', '2', '3', 'A' ],
  [ '4', '5', '6', 'B' ],
  [ '7', '8', '9', 'C' ],
  [ '*', '0', '#', 'D' ],
];
wary cloak
#

Ok now the pins

#
let rows = [
        pins.d26.into_pull_up_input().downgrade(),
        pins.d27.into_pull_up_input().downgrade(),
        pins.d28.into_pull_up_input().downgrade(),
        pins.d29.into_pull_up_input().downgrade(),
    ];
#

Why downgrade?

coral geyser
#

so you can put them all in an array together

wary cloak
#

Oh ok

#

And how do I make the others open drain outputs?

#

into_open_drain_output?

coral geyser
wary cloak
#
let columns = [
        pins.d22.into_opendrain().downgrade(),
        pins.d23.into_opendrain().downgrade(),
        pins.d24.into_opendrain().downgrade(),
        pins.d25.into_opendrain().downgrade(),
    ];
#

And we can finally call new

coral geyser
wary cloak
#

But is public-

coral geyser
wary cloak
#
Checking keypad v0.1.0 (C:\Users\fuchi\OneDrive\Documentos\Codigos_Rust\keypad)
error[E0425]: cannot find function `new` in module `Keypad`
  --> src\main.rs:42:26
   |
42 |     let keypad = Keypad::new(rows, columns, keys);
   |                          ^^^ not found in `Keypad`

For more information about this error, try `rustc --explain E0425`.
error: could not compile `keypad` due to previous error
#
impl Keypad {
    pub fn new(rows: RowPins, columns: ColumnPins, matrix: Matrix) -> Self {
        Self {
            rows,
            columns,
            matrix,
        }
    }
#

It doesn't make sense to me

coral geyser
#

oh, it thinks Keypad is a module

wary cloak
#

Isn't it?

coral geyser
#

keypad::Keypad::new()

wary cloak
#

Wait now that I think about it

#

I didn't make a use ::crate::Keypad::*

#

Or idk how it was it

#

Let's go one by one

wary cloak
coral geyser
wary cloak
#

The module is called Keypad

#

Like the Struct

coral geyser
wary cloak
#

Yes

coral geyser
#

oh, then in that case it's Keypad::Keypad::new()

#

module::struct::method

wary cloak
#

But without let to declare a variable to store it then..?

wary cloak
#

Or I mean

#

Why does this happen

#

Because it's named the same the struct and module?

coral geyser
#

though... it has a name conflict then

#

which is why modules are usually snake case

wary cloak
#

Yeah but it is one word(?

#

I could call it Matricial_Keypad

#

And the use the use statement above

coral geyser
#

no uppercase

wary cloak
#

Well well

#

matricial_ keypad

#

How do I make the use statement I tried to refer to?

coral geyser
wary cloak
#

Yeah ig

#

Idk why but it moved places

#

Pls

#

How do I get the module about main

#

Above*

coral geyser
wary cloak
#

Oh that's why

coral geyser
#

it helps a lot when you have like 20-100 files in one folder

wary cloak
#

Ok finally

#

We can move to loop

#

And test

#

I guess what I would do

#

Is create a variable that stores the result from calling get key

#

And then print it to serial port

#

Same for the rest basically

coral geyser
#

If you use the get string one, you can test all 3 at the same time

wary cloak
#

Well ig why not

#

So I should declare a buffer above first?

coral geyser
coral geyser
#
let buffer = ['X'; 20];

or something

wary cloak
#

Oh I have to

#

Initialize it?

coral geyser
wary cloak
#

The second one is the size right?

coral geyser
#

yep

wary cloak
#

Ok time to get testing

#

Ok now I just have to remember how to print things I will see the last project

#

How did I solve this?

coral geyser
#

because it's not a &str

wary cloak
#

But what about the second that is a usize?

coral geyser
#

I think that works

wary cloak
#

I added the use statement needed btw

#

But seems it's gonna be a pain

coral geyser
wary cloak
#

It wrote so many stuff..

coral geyser
wary cloak
#
Checking keypad v0.1.0 (C:\Users\fuchi\OneDrive\Documentos\Codigos_Rust\keypad)
error[E0277]: the trait bound `char: uDebug` is not satisfied
  --> src\main.rs:50:9
   |
50 |         uwriteln!(&mut serial, "{:?}", buffer).void_unwrap();
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `uDebug` is not implemented for `char`
   |
   = help: the following other types implement trait `uDebug`:
             &T
             &mut T
             ()
             (A, B)
             (A, B, C)
             (A, B, C, D)
             (A, B, C, D, E)
             (A, B, C, D, E, F)
           and 76 others
   = note: required because of the requirements on the impl of `uDebug` for `[char; 16]`
   = note: this error originates in the macro `proc_macro_call` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `void_unwrap` found for enum `Result` in the current scope
  --> src\main.rs:50:48
   |
50 |         uwriteln!(&mut serial, "{:?}", buffer).void_unwrap();
   |                                                ^^^^^^^^^^^
   |
  ::: C:\Users\fuchi\.cargo\registry\src\github.com-1ecc6299db9ec823\void-1.0.2\src\lib.rs:87:8
   |
87 |     fn void_unwrap(self) -> T;
   |        ----------- the method is available for `Result<(), void::Void>` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
4  | use arduino_hal::prelude::*; // trait ResultVoidExt
   |
help: there is an associated function with a similar name
   |
#
50 |         uwriteln!(&mut serial, "{:?}", buffer).unwrap();
   |                                                ~~~~~~

error[E0599]: no method named `void_unwrap` found for enum `Result` in the current scope
  --> src\main.rs:52:51
   |
52 |         uwriteln!(&mut serial, "{}", buffer_size).void_unwrap();
   |                                                   ^^^^^^^^^^^
   |
  ::: C:\Users\fuchi\.cargo\registry\src\github.com-1ecc6299db9ec823\void-1.0.2\src\lib.rs:87:8
   |
87 |     fn void_unwrap(self) -> T;
   |        ----------- the method is available for `Result<(), void::Void>` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
4  | use arduino_hal::prelude::*; // trait ResultVoidExt
   |
help: there is an associated function with a similar name
   |
52 |         uwriteln!(&mut serial, "{}", buffer_size).unwrap();
   |                                                   ~~~~~~

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `keypad` due to 3 previous errors
coral geyser
#

add use arduino_hal::prelude::*; ?

coral geyser
wary cloak
#

?

coral geyser
wary cloak
#

That solve it for the second one

coral geyser
wary cloak
#

How do we do the first?

#

Yeah but

#

How(?

coral geyser
#
for c in &buffer {
  uwrite!(&mut serial, "{}", c).void_unwrap();
}
wary cloak
#

Ok seems to be done

#

Also seems 0 warnings

#

So time to finally actually test

#

Mmm..

#

It finally executed so I pressed

#

From 1 to 9

#

Then # which is the one I choose to end

#

And it seems as nothing happened

#

Oh

#

I didn't change your 'a'

#

In the new version

#

It still didn't do anything

#

Now I'm going to press each one just in case

#

Nothing

#

I'm gonna execute it again with my change

coral geyser
#

try pressing them 20+ times so that it triggers the auto stop

wary cloak
#

Because maybe pressing A gives an 'A' and not an 'a'

wary cloak
coral geyser
wary cloak
#

But forgot too when we simplified

#

I pressed a lot of times and nothing happened

#

So I'm gonna re run with my change just in case

#

Ok let's see

#

No nothing happened

#

I pressed 0 to 9

#

And then #

#

Nothing

coral geyser
#

hmm, cool now you try get_key

#

see if we can figure out the issue

wary cloak
#

And pressing a lot of times also doesn't work

#

Ok

#
let key = keypad.get_key();

uwriteln!(&mut serial, "{}", key).void_unwrap();
wary cloak
#

Yeah I saw-

#

What do I do?

coral geyser
#
if let Some(key) = keypad.get_key() {
  uwriteln!(&mut serial, "{}", key).void_unwrap();
} else {
  uwriteln!(&mut serial, "none").void_unwrap();
}
wary cloak
#

Ok let's see

#
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: InvalidData, message: "Windows stdio in console mode does not support writing non-UTF-8 byte sequences" }', C:\Users\fuchi\.cargo\registry\src\github.com-1ecc6299db9ec823\ravedude-0.1.4\src\console.rs:27:45
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#

Didn't touch anything

coral geyser
wary cloak
#

Oh I thought the problem was with the function and all

coral geyser
#

apparently the arduino sent back invalid utf8

wary cloak
#

Maybe because

#

As it doesn't wait

#

It send none

coral geyser
#

oh, yeah

#

it could be overloading the serial port

wary cloak
#

That last part I didn't get

coral geyser
#

as it's writing so fast

wary cloak
#

Oh

coral geyser
#

The serial port on windows can only hold so much data

#

try adding a delay_ms(500); in the loop

wary cloak
#

Which one because there are 2 for loops

#
pub fn get_key(&mut self) -> Option<char> {
        for col in 0..self.columns.len() {
            for row in 0..self.rows.len() {
                if self.check_row_col(row, col) == true {
                    return Some(self.matrix[row][col]);
                }
            }
        }
        None
    }
wary cloak
#

Oh

coral geyser
#

between calls to get_key

#

because right now it's running at like 1 million attempts per second

wary cloak
#

Now it doesn't panic but

#

It doesn't do anything when I press

coral geyser
#

just a long list of none?

wary cloak
#

No just nothing

#

Nothing appears

coral geyser
#

oh

#

it's stuck somewhere then or it panicked

wary cloak
#

Wouldn't it tell me if it paniced?

coral geyser
wary cloak
coral geyser
#

it just locks up if that happens

coral geyser
wary cloak
#

I finished the program but the rx led is blinking

coral geyser
#

from the computer

#

tx would be sending to the computer

wary cloak
#

it is so annoying to go copy this each time cargo run -- --port COM6 -c

coral geyser
#

to get your history of commands

wary cloak
#

Oh

#

Weird..

#

it does not print it

coral geyser
wary cloak
#

No

#

I didn't see if it did

#

Pressing a key doesn't turn it on either

coral geyser
#

in any case lets simplify even more

loop {
  delay_ms(500);
  uwriteln!(&mut serial, "hi").unwrap();
}

an comment out the making of the keypad and the pin setup

wary cloak
#

Wait

#

Now it printed

#

Maybe it's because of how slow my computer is? Remember when I said the the lcd took it's time to answer to my commands? Tho not this long

#

hi
none
hi
none
hi
none
hi
none
hi
none
hi

#

Goes like this for a while

coral geyser
#

what happens if you press a key?

wary cloak
#

Tho I don't see it you know

#

Keep going

#

It just appeard out of knowhere

#

And it's doing nothing again

wary cloak
coral geyser
#

maybe we overloaded the windows serial driver ferrisballSweat

wary cloak
#

I pressed all keys but I didn't see the tx turn on and it didn't do anything

#

It's likely because it has a million more none's to print

#

Do we maybe try wait for key?

wary cloak
#

Ok

coral geyser
#

we need to get to a known working state

wary cloak
#

Mmm..

#

It doesn't work ferrisballSweat

#

Or again it's going to take like 5 minutes and then a lot appear

#

I have a suggestion

#

Mmm ..

#

I mean

#

I could call get_key, bind it to a variable and then do a match expression maybe where either result makes a break, a catch all that says I received none and another one for a specific key idk..

#

Thing is

#

We are printint to much to the serial port ig

#

Printing*

#

Too fast*

#

I'm gonna try again with a delay like 5000000000

#

Or smth

#

To see if it prints one time at least

#

Well it allows 50000 only more is out of bounds

#

And it also didn't work

#

Btw now rx is no longer blinking-

#

Oh there it is

#

One hi

#

Oh ofc

#

The delay is before

#

So it waited 50000 miliseconds

#

And then printed hi

#

And it did again :o

#

Weird this time it took like way less

#

Oh when I sent this

#

It's now another one

#

As I thought the problem is just how fast we are printing ig?

#

Ig last time

#

Since it printed it once and then entered another loop

#

It didn't happen

#

So knowing this and having tested that..

#

What do we do now?

#

Even know it really takes it's time

#

And I can't use a much higher delay anyways

#

Actually

#

It haven't printed another one

#

Is rather inconsistent

#

Oh

#

It printed so more and I didn't notice

#

13 times

#

Now it printed more

#

So yeah

#

It seems to be doing fine

#

With almost the highest delay I can perform.

#

So now that we know and tested this.. What now?

wary cloak
#

You know I think that it may work for what we need it, it likely works but no clue how to test it

#

Could maybe test driven development do it? Like calling wait_for_key in a asserteq! And compare it to an specific key I know I will press idk

#

Because it seems like for the non-waiting nature of get_key it seems useless or impossible to test

#

I think testing wait_for_key which is the one we will indeed use should do and if it works then get key too

wary cloak
#

Or maybe we could try them in setup so it's just once

wary cloak
#

Are you available?

#

Oh forgot

#

@coral geyser

coral geyser
#

Sort of

wary cloak
#

Noooo ai wasn't heree

wary cloak
#

Just a little bit after you left with it working with the highest delay possible

#

And either the test driven development solution or try it in setup solution

coral geyser
wary cloak
#

I was using serial just now

#

For the normal arduino version of this project

#

Trying to figure it why some code related to the rfid isn't working, I'm frustrated-

coral geyser
#

Yeah, so I am wondering if the ravedude console is messed up on windows

wary cloak
#

Hmm

#

That's why I said maybe we should try it in setup or smth because maybe we are losing time for something that already works

coral geyser
#

You can do that also. It's up to you as it's your project. I just want to know why it doesn't work properly

wary cloak
#

We can do all test you want if you have another idea

wary cloak
#

What but

#

Wait*

#

Would that work I mean

#

I just open an arduino IDEA

#

Ide*

#

With no project

#

And open the console?

coral geyser
#

Yep

wary cloak
#

While the program is running

#

It works if it has a project?

#

I don't want to create a new one

coral geyser
#

The Arduino IDE should auto connect

wary cloak
#

Like this one here

coral geyser
wary cloak
#

If it's just for opening the ide

#

Ok now I open vs

#

arduino_hal::delay_ms(500);
uwriteln!(&mut serial, "hi").unwrap();

#

this one?

coral geyser
#

Yep

wary cloak
#

ok let s see

#

I get that message

coral geyser
wary cloak
#

Ah no

#

Doesn't that stop the program?

coral geyser
#

Nope

#

The Arduino will just keep going

wary cloak
#

Keeps printing that

coral geyser
#

Set the baud rate to 57600

#

By default it's probably 9600

wary cloak
#

Oh it works

coral geyser
#

Cool, so it's just the ravedude console

#

And I think I know why

#

Go back to the rust code and add a \r to the end of hi

#

Then program it again

wary cloak
#

Do we test the original with buffer buffer size the for c in buffer and uwriteln?

wary cloak
#

Ok*

#

It's still the same

coral geyser
wary cloak
#

I should close my arduino IDE windows?

coral geyser
wary cloak
#

I did that

coral geyser
#

And check that the command actually programmed (scroll up)

wary cloak
#

I mean I looked at it trigger the warnings for unused code and all but then I see this

coral geyser
#

Ok, so it's not going to be a simple fix ferrisSob

wary cloak
#

I mean so we need to?

coral geyser
#

We will just use the Arduino console

wary cloak
#

We could just see if it works in that

#

Yeah

#

So do I test

coral geyser
#

Yep

#

Back to testing get key

wary cloak
#
let mut buffer = ['x'; 16];
        let buffer_size = keypad.get_char_array(&mut buffer);

        for c in &buffer {
            uwriteln!(&mut serial, "{}", c).void_unwrap();
        }

        uwriteln!(&mut serial, "{}", buffer_size).void_unwrap();
#

this one

#

Well it didn't work

#

Ok then let's test

#
if let Some(key) = keypad.get_key() {
            uwriteln!(&mut serial, "{}", key as u8).void_unwrap();
        } else {
            uwriteln!(&mut serial, "none").void_unwrap();
        }
        arduino_hal::delay_ms(500);
#

This one

coral geyser
wary cloak
#

It keeps printing none

#

No matter how much times I press

#

Or keep it presses

#

Pressed*

coral geyser
#

Sadness ferrisSob

#

All the pins are correct right?

wary cloak
#

Yeah because it works in the arsuino version I tested it last Wednesday

#

And the code worked with that pins

wary cloak
#

There you have in any case

coral geyser
#

Hmm, maybe we test check_row_col

wary cloak
#

For now I need to leave, eat something and bath after that I continue

#

And also charge the computer while all of that happens

#

But could you leave me what we are going to do?

#

Like

#

The code to test because

#

Idk how we could test that function

#

Btw I have a suggestion

#

Even tho my c program works and it's good reference to see how to detect and print a key

#

Maybe we could also go to

#

The git repository

#

Of either the keypad.h Library I used for arduino

#

To see even tho in c++ how he did it

#

Or the one from the 4x3 one because it should be similar

wary cloak
#

@coral geyser aaaand I'm finally back, we can continue if you are

coral geyser
#

Please continue

wary cloak
#

Ok so while I'm turning the computer on

#

How do we check

wary cloak
#

Which is our most primitive function

#

I mean maybe we could print the result of calling our function which is a boolean and so start pushing first other keys to see if it's always false and then push the one I have to and see if it's true or smth like that

#

Just for refresh imma send it again

#
fn check_row_col(&mut self, row: usize, col: usize) -> bool {
        for (i, pin) in self.columns.iter_mut().enumerate() {
            if i == col {
                pin.set_low();
            } else {
                pin.set_high();
            }
        }

        self.rows[row].is_low()
    }
coral geyser
#

Sounds good

wary cloak
#

How do you that AP?

#

Btw I will have to make the function public for now

#
 uwriteln!(&mut serial, "{}", keypad.check_row_col);```
#

Why is this wrong?

coral geyser
wary cloak
#

Oh yeah

#

That's why

wary cloak
#

So

#

0 to 3

#

I guess

#

Let's make A the one, so row 1 column 3

coral geyser
#

Yep

wary cloak
#
uwriteln!(&mut serial, "{}", keypad.check_row_col(1,3)).void_unwrap();
#

here it is

#

Ok let's see

#
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: InvalidData, message: "Windows stdio in console mode does not support writing non-UTF-8 byte sequences" }', C:\Users\fuchi\.cargo\registry\src\github.com-1ecc6299db9ec823\ravedude-0.1.4\src\console.rs:27:45
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#

Does this mean I don't try the arduino ide?

coral geyser
#

Open the Arduino console

#

If it programmed then you are good

wary cloak
#

It's printing to fast

coral geyser
#

Delay_ms(...) ferrisGlasses

wary cloak
#

:0

wary cloak
#

It works

#

Tho it seems I miss calculates

#

Calculated*

#

If I press B, not A

#

I see true

#

If nothing false

#

Let me see if nothing else triggers true

coral geyser
#

So we can detect buttons ferrisGlasses

wary cloak
#

And it doesn't get trigger by anything else so yeah

#

Check_row_col works as intended

wary cloak
#

Ok fine

#

So the problem it's get key

wary cloak
#

Thing is.. What's wrong?

coral geyser
#

Oh I think I know

wary cloak
#

The fact that is an optional? It makes sense for it to be, how we print it?

#

Oh tell me

coral geyser
#

We didn't take into account the speed of electricity

#

It's switching the pins to fast

#

So they haven't finished being set to 0v before it reads the input pin

wary cloak
#

I didn't understand

#

So you mean..

coral geyser
#

So if we add a small delay, something like delay_us(100); between the setting low in check_row_col and reading the input it should work

wary cloak
#

That it changes state really fast and by the time we read it is no longer low..?

coral geyser
# wary cloak I didn't understand

The mega runs at 16 MHz which allows it to set the pin low, then read the input pin so fast that the electrical state of the wire hasn't changed yet

wary cloak
#

I get when talking about of state of the pin but, state of the wire?

coral geyser
coral geyser
#

Happens that that time is to slow for what we are doing

wary cloak
#

I really can't understand at all I just get that arduino too fast, keypad not so much?

#

It's a

#

Keypad thing

#

Or

coral geyser
#

It's a physics thing

wary cloak
#

Arduino is too fast for us

coral geyser
wary cloak
#

I didn't get that of it doesn't get to 0v in time

#

Just that?

coral geyser
#

I think that will behave better

wary cloak
#

So before the is low question?

coral geyser
wary cloak
#
pub fn check_row_col(&mut self, row: usize, col: usize) -> bool {
        for (i, pin) in self.columns.iter_mut().enumerate() {
            if i == col {
                pin.set_low();
            } else {
                pin.set_high();
            }
        }
        arduino_hal::delay_ms(500);
        self.rows[row].is_low()
    }
#

So now we try

coral geyser
#

Basically what is happening is we set the column to 0v, but by the time the input is read it's at like 4.2v and dropping

coral geyser
wary cloak
#

Oh so..

#

In check row column

#

We ask for a column and if it's not we set it to 0

#

Really fast

#

The problem being

#

That then we immediately say

#

Hey it's 0?

#

And all that before it could physically be 0

#

Right?

coral geyser
#

Yep

#

Physics is getting in the way ferrisConcern

wary cloak
#

Well now we now check_row_col works now it's just a matter of get key

coral geyser
wary cloak
#

Ok so for now is printing none slower than before

coral geyser
coral geyser
wary cloak
#

Let the program speak for it itself

#

"none"

#

But we got a lower none printing speed rate(?

wary cloak
#

?

coral geyser
#

Try delay_us(100);

#

Which is 100 micro seconds

wary cloak
#

That slow is that many mili(?

coral geyser
wary cloak
#

No I mean

#

Why such a reduction in time

coral geyser
wary cloak
#

Still none

coral geyser
#

Right now it takes 0.5 s x 4 x 4 to read the whole keypad

#

Which is 8 seconds

coral geyser
wary cloak
#

Yes

coral geyser
#

Try delay_ms(10);

wary cloak
#

I'm not sure how this is going to make it work-

coral geyser
#

If it doesn't I have more ideas for things to check

#

I would normally measure the time delay we need, but ... I am not there ๐Ÿคทโ€โ™‚๏ธ

wary cloak
#

Sure I was just curious on the logic behind that being the issue for learning purposws

#

Still none

#

God damn

coral geyser
#

K, now setup a test with 2 check_row_col at different spots

wary cloak
#

What

coral geyser
#

Like how you had it checking just A

#

Do that and check another spot

wary cloak
#

Oh

#
uwriteln!(&mut serial, "{}", keypad.check_row_col(1,3)).void_unwrap();

        uwriteln!(&mut serial, "{}", keypad.check_row_col(3,2)).void_unwrap();
#

Like that?

#

Now it doesn't work

#

Or it's too fast for me to see the true

#
uwriteln!(&mut serial, "{}", keypad.check_row_col(1,3)).void_unwrap();
        arduino_hal::delay_ms(500);

        uwriteln!(&mut serial, "{}", keypad.check_row_col(3,2)).void_unwrap();
        arduino_hal::delay_ms(500);
coral geyser
#

Hmm

wary cloak
#

Like that?

coral geyser
#

Yeah

wary cloak
#

Wtf doesn't it work now-

#

It doesn't make sense -

#

Aha

#

Mm

#

I re opened and pressed really fast and saw only one true

coral geyser
#

Hold the key

wary cloak
#

I did the same for 8 which should be the other unless I'm wrong again

#

And it didn't work

#

Now I'm going to see if what I did to B works again first

#

I did some tries that didn't work

#

Now I made it again

#

If I hold it

#

I still only see false

#

Let's see if so that of opening already pressing but holding

#

It works

coral geyser
#

I wish we had an oscilloscope

wary cloak
#

If I hold the button

#

And enter the console

#

It says true

#

But only once

#

It doesn't make any sense to me-

#

And also it works with B not 8

coral geyser
wary cloak
#

So it do seems that the problem still is in check row col

coral geyser
#

Go back to 1 check_row_col

wary cloak
#

But it's just

#

With 1 it works, with 2 not, but if I hold press is true only before entering and then never more but with the other it doesn't work

wary cloak
#

It works perfectly fine

#

Holding or not

#

Mmm

#

Sometimes I pressed and it didn't

#

Now I pressed three times

#

And then it started working fine with each press

#

But those before didn't work

#

Now I pressed a lot of times

#

It's kind of inconsistent to start detecting

coral geyser
#

That's expected with the delay

wary cloak
#

But holding seems to he consistent

coral geyser
#

It only reads it every 0.5 s

wary cloak
#

It works with one at a time tho isn't that how it normally works anyway?

coral geyser
#

Set that delay to like delay_ms(100) and you should see more consistent results

#

Because it will read fast and report more often

wary cloak
#

Sure but what do we do now

coral geyser
wary cloak
#

Well that wouldn't make sense since other changes we made did

#

And I have autosave

coral geyser
#

Yeah

wary cloak
#

And also I had the slower none

coral geyser
#

But like I have done this exact thing many times and never seen this

wary cloak
#

I'm sending the function again in any case

coral geyser
#

And I even double checked the rust keypad crate and the Arduino keypad library

wary cloak
#
pub fn check_row_col(&mut self, row: usize, col: usize) -> bool {
        for (i, pin) in self.columns.iter_mut().enumerate() {
            if i == col {
                pin.set_low();
            } else {
                pin.set_high();
            }
        }
        arduino_hal::delay_ms(10);
        self.rows[row].is_low()
    }
#

I am thinking

#

If get key gives back wrong the key pressed but that isn't it because even if it did it would show the key just the incorrect one

#

But I'm gonna express it anyways

#
pub fn get_key(&mut self) -> Option<char> {
        for col in 0..self.columns.len() {
            for row in 0..self.rows.len() {
                if self.check_row_col(row, col) == true {
                    return Some(self.matrix[row][col]);
                }
            }
        }
        None
    }
#
let keys = [
        [ '1', '2', '3', 'A' ],
        [ '4', '5', '6', 'B' ],
        [ '7', '8', '9', 'C' ],
        [ '*', '0', '#', 'D' ],
    ];
#

Say I pressed 2

#

So col would 1 at it's second iteration

#

And row would be 0 at it's first iteration

#

So it would give back

#
Some(self.matrix[0][1]) 
#

...

#

Why when I thought about it

#

It seemed wrong

#

But now I see it's fine no way-

#

Well forget all you just saw

#

Anyways if you needed it there are the two criminals check and get

coral geyser
#

I am tempted to pull out my oscilloscope to check this all

wary cloak
#

My school has (?

#

But

coral geyser
#

It will take me a minute to wire everything up for it

wary cloak
#

It's not like I'm gonna be using it while chatting on discord and stuff

wary cloak
#

Man so many thing I gave for granted with arduino

#

Btw..should I upload something else to the board? Because idk if it's good to keep communication for so long, seeing that led blinking non stop get's me worried

coral geyser
#

could you zip up the entire project and send it to me?

wary cloak
#

How do I?

coral geyser
#

in windows explorer, right click the folder, then send to, compressed (zip) folder

#

without the target/ folder preferably

wary cloak
#

Taking it's tike to delete target

coral geyser
#

alright I can replicate the behavior

#

time for the oscilloscope

wary cloak
coral geyser
# wary cloak wdym?

I setup my mega with a 4x4 keypad to match yours and it behaves the same way (only false with 2 check row col)

wary cloak
#

so lucky to meet a real embedded developer with stuff at home(?

coral geyser
#

this is with one check_row_col

wary cloak
#

ok

#

normal stuuf there

#

stuff*

coral geyser
#

them voltages don't look right ferrisSus

wary cloak
#

what

#

it's always high?

coral geyser
#

yep

wary cloak
#

in our case always false?

coral geyser
#

yep

wary cloak
#

I mean

#

is it not what we already new?

#

knew*

coral geyser
#

I think I found the issue

wary cloak
#

Who was the agent of evil?

coral geyser
#

the hal

wary cloak
#

...

#

Again-?

coral geyser
#

There is a small difference with mega chips, their pins default to high when you switch them to output

#

The hal doesn't take this into account so when we use open drain instead of grounding it goes to 5v

wary cloak
#

wh-

#

Why the hell

#

Is that a thing

#

Why when they were doing the mega they thought

#

Yeah

#

Output defaults to 5 volts

wary cloak
#

So let's see

#

Normally

coral geyser
#

this is what we want

wary cloak
#

A microcontroller would have output a 0v by default unless I do something because it's idk what it makes sense

#

So open drain

#

Disconnects them completely

#

And when low they are nothing when high they are 0

#

But in this case

coral geyser
#

this is what we get

wary cloak
#

Since MEGA is a special boy

#

He decided that outputs are by default 5volts unless we tell them otherwise

#

But.

#

How does this affect open drain?

#

I told until the part that made sense to me

#

Or I thought I did understand

coral geyser
#

how open drain is implemented on the mega is it switches from output to input

#

output for the ground

#

input for floating

wary cloak
#

What

#

It's hard to understand because of how it doesn't make sense

wary cloak
#

Ok so open drain has as different behavior

#

It turns my output into a input..?

coral geyser
#

so when it sets it up, it makes it a output set to 0v. So then everything worked, but as soon as we set it to high (open) it becomes an input that just floats. But setting back to low makes it an output at 5v. Hence why as soon as we add another check_row_col everything breaks

wary cloak
#

Wait so..

#

I get mixed all up

#

So low instead of disconnecting

#

Set the output to 0 volt?

coral geyser
#

yeah

#

which we need to pull down the input pins

wary cloak
#

Why didn't I get a short circuit? Luck?

coral geyser
#

everything just stays at 5v

wary cloak
#

So then when set to high

#

Instead of 0v

#

It magically turned

#

Into a floating input

coral geyser
#

yep

wary cloak
#

Which if I remember correctly could be whatever because of noise

coral geyser
#
// bitMap stores ALL the keys that are being pressed.
for (byte c=0; c<sizeKpd.columns; c++) {
    pin_mode(columnPins[c],OUTPUT);
    pin_write(columnPins[c], LOW);    // Begin column pulse output.
    for (byte r=0; r<sizeKpd.rows; r++) {
        bitWrite(bitMap[r], c, !pin_read(rowPins[r]));  // keypress is active low so invert to high.
    }
    // Set pin to high impedance input. Effectively ends column pulse.
    pin_write(columnPins[c],HIGH);
    pin_mode(columnPins[c],INPUT);
}

this is the keypad library you are using in the arduino code

#

see the pin_mod and pin_writes

#

they do this logic

wary cloak
#

And when set back to low is again an output of 5 volt because of the strange switch and no longer 0 volt? Because of the input output casting?

wary cloak
#

Now the only thing left to understand is

#

Why one worked perfectly

coral geyser
wary cloak
#

Oh that input remains low

#

In get key

#

All get high

#

And then we are done

#

And with two well one of them would set to high and there's no going back

#

Ok I think I understood the logic of our problem

#

Now the thing is the solution

#

Being aware to tell it

#

Hey be an output 0 in each iteration?

#

Or smth

coral geyser
#

I think the simplest thing is to clone the hal repo and make the edit to fix it

wary cloak
#

As long as it works and I understand the code and why it works

coral geyser
wary cloak
#

Maybe it's another, but it doesn't matter

#

So..

#

We have to open the docs or smth?

wary cloak
#

sure

#

What now?

coral geyser
wary cloak
#

Which one (?

coral geyser
#

the one that makes a new folder for it

wary cloak
#

?

#

What now?

coral geyser
wary cloak
#

Ok done

#

Now?

coral geyser
# wary cloak Now?

in cargo.toml edit it to have

[dependencies.arduino-hal]
path = "avr-hal"
features = ["arduino-mega2560"]
#

then try a cargo check

wary cloak
#

The one inside that folder?

#

That one?

coral geyser
#

yep

#

oh also the code all works besides this

coral geyser
coral geyser
coral geyser
wary cloak
#

The I replace?

coral geyser
#

yep there

#

replace that

#

this will use your local copy of the crate instead of the one on github

wary cloak
#

Ok done

coral geyser
#

cargo check

wary cloak
#

Error

#
PS C:\Users\fuchi\OneDrive\Documentos\Codigos_Rust\keypad> cargo check
error: failed to get `arduino-hal` as a dependency of package `keypad v0.1.0 (C:\Users\fuchi\OneDrive\Documentos\Codigos_Rust\keypad)`

Caused by:
  failed to load source for dependency `arduino-hal`

Caused by:
  Unable to update C:\Users\fuchi\OneDrive\Documentos\Codigos_Rust\keypad\avr-hal

Caused by:
  found a virtual manifest at `C:\Users\fuchi\OneDrive\Documentos\Codigos_Rust\keypad\avr-hal\Cargo.toml` instead of a package manifest. 
coral geyser
#

path = "avr-hal/arduino-hal"

#

try changing it to that

wary cloak
#

Blocking waiting for file lock on build directory

#

waiting

#

Still waiting

#

Any moment now

#

Finally god

#

4 minutes

#

I see a green finished so

#

It should be fine

#

Now that we finally got this

#

What do we do?

#

And what does this repo has btw? What do I have know?

coral geyser
wary cloak
#

Oh-

coral geyser
wary cloak
#

That seems quite a lot-

coral geyser
#

go to avr-hal/avr-hal-generic/src

wary cloak
#

Ok now where in src?

#

Port?

coral geyser
wary cloak
#

Done

coral geyser
#

scroll to line 347

wary cloak
#

See it

#

My first unsafe I see ferrisHmm

coral geyser
#
 /// Set pin low (pull it to GND, Output to low).
#[inline]
pub fn set_low(&mut self) {
    unsafe { self.pin.make_output() }
    unsafe { self.pin.out_clear() }
}

is what it should be

wary cloak
#

So in the end we use unsafe

coral geyser
# wary cloak

yeah add the unsafe { self.pin.out_clear() } line to the function

wary cloak
#

Done

coral geyser
wary cloak
#

Which one

#

Do we try the get char array one

#

Or the get key one

coral geyser
#

which ever

wary cloak
#

Ok then get char array because it's the "all works" Scenario

#

Ok so in the meantime

coral geyser
wary cloak
#

What did we do, I mean ik change the behavior of the function to dealt with I/O bullshit but how did we solve it

coral geyser
wary cloak
#

So the behavior is

#

The same as before

#

But when it comes back from input floating to output

#

The clear forces it to always be 0v?

coral geyser
#

yep

wary cloak
#

It works better but still not quite

#

I pressed

#

0 to 9

#

Then # to exit

#

This got printed

#

0
1
3
5
6
7
8
9
x
x
x
x
x
x
x
x
8

coral geyser
#

we probably need to change the delay in check_row_col

#

do be less

#

I have it at delay_us(1); in my version

wary cloak
#

Btw I don't get still how it works I mean, to match a key we needed 0v as a high, how does having a floating input that could be whatever because of a noise match?

#

And didn't we want to disconnect it instead of having 0 volts when low? Because now we have 0 volts when low, why no short circuit?

coral geyser
coral geyser
wary cloak
#

So it's the traditional high low now?

coral geyser
wary cloak
#

Yeah that I get

#

That's rows

#

But columns

#

High is 5 volts because is a floating pin input

#

And low 0volt output

coral geyser
#

before it was 5v floating high, 5v low

wary cloak
#

...

#

Yes I get the but

#

Wait it failed now

coral geyser
wary cloak
#

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: Controlador no vรŸlido.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

avrdude done. Thank you.

Error: avrdude failed

#

That but more times

coral geyser
#

run it again

#

or you have the arduino console window open

wary cloak
# coral geyser ^ hence the weird wiggle around 5v

But I mean a it's supposed to work with columns being low disconnected high 0 volts to match the floating input and to avoid short circuit, now it's low 0volt and high 5 volts so I don't know how that still works

wary cloak
#

I did 1 through 9 this time

coral geyser
wary cloak
#

1
2
3
4
5
6
7
8
9
x
x
x
x
x
x
x
9

#

It's better but

#

You know

wary cloak
wary cloak
coral geyser
wary cloak
#

Oh and I set to X if it wasn't filled

#

Yeah it works perfectly

#

Finally ferrisHot ferrisHot

wary cloak
#

Wdym sorry

coral geyser
#

didn't even think to question the hal library

wary cloak
#

It was so goddamn hard to figure this out

#

Because it make so much no sense

#

It's titanic you solved it

coral geyser
wary cloak
#

So in our case it's 0?

coral geyser
wary cloak
coral geyser
#

which fixes the shorting issue

#

as it's a short only if a lot of current flows

wary cloak
#

So..

#

Our high ends up being a 0 volts as it should be..?

coral geyser
#

effectively 0v

wary cloak
#

But isn't low also 0?

coral geyser
wary cloak
#

Now I know there are different types of 0-

#

Oh

coral geyser
#

the high one can get dragged around by the input's pullup

wary cloak
#

Clear makes it nothing like the drain behavior?

coral geyser
#

the actual low 0v can drag the input's pullup to 0v

coral geyser
wary cloak
#

What's the difference between hard 0 volt and the usual 0 volt of our high?

coral geyser
wary cloak
#

Ok

#

Tho I still don't get why our high is 0 but just thought it would be easier to understand our low

#

Well I'll be turning off my computer so after understanding how this works we go to sleep (?

coral geyser
#
| Set Output | Pressed | Input | Real Output          |
|------------|---------|-------|----------------------|
| Low        | No      | High  | Low                  |
| High       | No      | High  | Between Low and High |
| Low        | Yes     | Low   | Low                  |
| High       | Yes     | High  | High                 |
wary cloak
#

Let me see it on the pc because cell phone doesn't make it justice

coral geyser
#

that should be better

wary cloak
#

Mm..

#

Could you explain to me what I'm seeing?

coral geyser
#

"Set Output" is what the code sets the output pin to be

wary cloak
#

Which code, ours now?

coral geyser
#

"Pressed" is whether the button connecting output to input is pressed

#

"Input" is the input pin

coral geyser
#

"Real Output" is what you would measure with an oscilloscope on the output pin

wary cloak
#

Oh ok so when we set out output to low and there's no button press we have a high input because it turns it into a floating input, that's correct?

#

Wait

#

Join

#

No*

#

That's when high

#

Let's start again.

#

So we set our column pin which is an output to low

#

And no button is pressed

#

Why is high when low, weren't we setting it to hard 0?

coral geyser
#

because the button isn't pressed the output isn't connected to the input pin

#

like electrically

wary cloak
#

Yes I get that

coral geyser
#

and the input defaults to high from being pulled up

wary cloak
#

Oh

#

We are seeing

#

Rows and column behavior

#

Ok now I may understand

#

Ok so the input defaults to high because it is pull up

#

So in the first to cases I know it's high for that and I'm ignoring it

#

No button pressed yet

#

When my output it's low it's and absolute 0 that doesn't match with 5 volts so the real output is now

#

Low*

#

I get that

#

Then when we set it to high, there's a high-high match?

#

I mean..

#

Input pull up defaults to 5 volts

#

Now.

#

When we

#

Have our column pin high

#

It turn into a floating input which can have whatever voltage, right?

coral geyser
wary cloak
#

..

wary cloak
#

So

#

What voltages has in our case when high?

coral geyser
wary cloak
#

Why 5 volts?

coral geyser
#

high is 5v

wary cloak
#

Oh for that it's a normal pin

#

Ok I get that

#

So

#

We would have a 5 volts matching with another 5 volts

#

What happens?

coral geyser
#

nothing, we happy because the voltages match so no current flow

wary cloak
#

But since they match isn't it like a key pressed or our code it's like no if it's not two low matches it's not a key?

coral geyser
#

the code will consider a high as nothing being pressed

wary cloak
#

Ok

coral geyser
#

which is good when column is set to high because that's a "disabled" column

wary cloak
#

But what about that between low and high? Does that matter?

coral geyser
#

that we aren't checking right now

coral geyser
#

But the only thing that is giving 5v (the inputs)

#

can only provide a tiny amount of current

#

so nothing bad happens

#

where as if the columns also provided 5v as an output they would provide enough current to be an issue

wary cloak
#

I was speaking about what it said in the real output but since you mention it

coral geyser
coral geyser
#

in the same row

wary cloak
#

No, how does it set it all to 0

#

I don't get our anti short circuit now

coral geyser
#

so it overrides the weak 5v pullup in the inputs

wary cloak
#

..

#

More or less

#

So let's say we have a button pressed

coral geyser
wary cloak
#

Our column is low so 0 volts to the 0 volts of the input that it's now low

#

Why the real output it's low? Isn't this the key pressed scenario?

coral geyser
wary cloak
#

But the other cases where key not pressed and this one it is?

#

Were*

coral geyser
#

it will be something like 4.5v in reality

#

not actually 0v or 5v

wary cloak
#

So when the pin is high which is the case we don't want it to match it's not 0 so it wouldn't match the 0-0 as before so it's fine, and when it's low it would match the 0-0 and well the in our behavies the same way

#

So.

#

Now instead of asking for the column

#

With the pin on high

#

That was 0 volts, we do it with low our now 0 volts?