Struggle connecting Arduino Leonardo with Wii Nunchuk to Rust code

Right now, I'm using serial_port crate, but I don't really know how to use it correctly. The code so far looks like this:

let mut serial = File::open("/dev/ttyACM0").unwrap();
let timeout = Duration::from_secs(1);
let mut reader = BufReader::new(serial);
let mut buffer = String::new();

    match reader.read_line(&mut buffer) {
        Ok(_) => {
            let data: Vec<&str> = buffer.split(",").collect();
            if data.len() != 7 {
                println!("Invalid data received: {:?}", buffer);
            let x_joystick: f32 = data[0].parse().unwrap();
            let y_joystick: f32 = data[1].parse().unwrap();
            let x_accelerometer: f32 = data[2].parse().unwrap();
            let y_accelerometer: f32 = data[3].parse().unwrap();
            let z_accelerometer: f32 = data[4].parse().unwrap();
            let c_button: f32 = data[5].parse().unwrap();
            let z_button: f32 = data[6].parse().unwrap();
        Err(e) => {
            match e.kind() {
                std::io::ErrorKind::TimedOut => {
                    println!("Timeout error: {:?}", e);
                    // You can handle the timeout error here
                _ => {
                    println!("Other error: {:?}", e);

This is the main connectio part of the code. The rest is using the input. I'm REALLY new to Rust, and a lot of this code I looked at and tried to piece it together. Pretty much, I want the data from the Arduino to be read and then to function pretty much as a joystick / game controller. Thing is, when I try to use x_joystick somewhere like this:

let mut lAxisX = x_joystick;

it doesn't work and tells me it can't be found or is in wrong scope.

Any help would be appreciated

I don't know about the Arduino library you're using, but you might have better luck finding someone who can help if you also copy-paste the specific error you're getting into your post.

At a glance, though, what you're describing sounds like you placed the line
let mut lAxisX = x_joystick;
beyond the valid scope of x_joystick.

As you've declared in your code, x_joystick only exists inside the match statement's Ok(_) arm. For instance...

let a  = "whatever";
   let b = "something";
   let c = a; // this works. a is in scope
   let c = b; // this works. b is in scope
let c = a; // this still works. a is in scope
let c = b; // this does not work. b was freed after the preceding '}'

This might be helpful too: Scoping rules - Rust By Example

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.