Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!

Tips & tricks for Arduino projects with GitHub Copilot AI

Copilot AI can assist you code your Arduino project, but sometimes it gets things wrong. Explore ways to help Copilot, help you!

BeginnerProtip1 hour1,503
Tips & tricks for Arduino projects with GitHub Copilot AI

Things used in this project

Software apps and online services

GitHub Account
GitHub Copilot subscription
VS Code
Microsoft VS Code
Visual Studio Code Extension for GitHub Copilot

Story

Read more

Code

Copilot for Arduino: Tips and Tricks

C/C++
// include the accelerometer library for the Arduino Nano RP2040 Connect
#include <Arduino_LSM6DSOX.h>

bool tiltState;

void setup() {
    // put your setup code here, to run once:

    // initialize the serial port
    Serial.begin(115200);

    // wait for the serial port to open
    while (!Serial);

    // initialize the accelerometer
    if (!IMU.begin()) {
        Serial.println("Failed to initialize IMU!");
        while (1);
    }

    // initialize the LED
    pinMode(LED_BUILTIN, OUTPUT);

    // Create a default tilt state variable of false
    tiltState = false;
}

bool tiltDetected()
{
    float x, y, z;

    // read the accelerometer
    IMU.readAcceleration(x, y, z);

    // check if the accelerometer is tilted
    if (abs(x) > 0.5 || abs(y) > 0.5) {
        return true;
    }
    else {
        return false;
    }
}

void loop() {
    // put your main code here, to run repeatedly:

    // Detect tilt on the Nano RP2040 Connect. Store the current detected tilt state in a local
    // variable.
    bool tilt = tiltDetected();

    // While the state is tilt, blink the LED 10 times per second.
    // While the state is not tilt, turn off the LED.
    if (tilt) {
        digitalWrite(LED_BUILTIN, HIGH);
        delay(100);
        digitalWrite(LED_BUILTIN, LOW);
        delay(100);
    }
    else {
        digitalWrite(LED_BUILTIN, LOW);
    } 

    // Compare the current tilt state to the previous tilt state. 
    // If the tilt state changed from not tilted to tilted, log once there is tilt detected.
    // If the state changed from tilted to not tilted, log once there is no tilt detected.
    // Store the current tilt state as the previous tilt state.
    if (tilt != tiltState) {
        if (tilt) {
            Serial.println("Tilt detected!");
        }
        else {
            Serial.println("No tilt detected!");
        }
        tiltState = tilt;
    } 
}

Credits

Mollie Muñoz
6 projects • 11 followers
A lifelong learner, creator, and problem solver for whatever adventure or new idea comes my way. Software Engineer ll @ Microsoft.
Contact
Jen Fox
35 projects • 144 followers
Dabbled in dark matter, settled into engineering w/ a blend of inventing and education! Sr.PM @ MicrosoftFounder/CEO of FoxBot Industries
Contact
Adrian Bonar
6 projects • 12 followers
Always looking to make positive impacts with technology on society, empower others, and grow through culture and engineering.
Contact
nberdy
5 projects • 6 followers
Contact
Mark Wilson-Thomas
2 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.