Giter Club home page Giter Club logo

cygni-arduino-robot's Introduction

cygni-arduino-robot

Cygni konsulentmøde der handler om arduino robot programmering. I dette tilfælde en line follower robot.

Setup Arduino workspace

  1. VS code can be used as an IDE for programming Arduinos but its a bit of a hassle to setup: guide
  2. But, arduino have their own IDE that can be found here

Code structure

All arduino sketches (programs) are created using .ino file. This will be the entry point for the program. As the arduino is programmed using a version of c++. Therefore packages containing header and implementation files can be used. These files needs to be located in a folder called src.

file structure:

- sketch
    - src
        - class.h
        - class.cpp
    - myFirstSketch.ino

Code examples

I/O pins Pins are used as the interface for all i/o operations. It is common de use a macro to define a pin.

#define LED_PIN 2 //Macro, now LED_PIN will always have the value 2

Arduino also requires a pin to have a mode (pinMode)

void setup(){
    pinMode(LED_PIN, OUTPUT);   //This sets the LED_PIN's mode to be an output, meaning the pin can send data/voltage out.

    pinMode(SENSOR_PIN, INPUT); //This sets the SENSOR_PINS's mode to be an input, meaning the pin is ready to read data/voltage.
}

There are two types of pins analog and digital pins. Analog pins can read a value between 0 and 1023 where digital pins can only read HIGH or LOW. Some digital pins can output a PWM (pulse width modulation) signal with a range from 0 - 255.

void loop(){
    bool sensor_value = digitalRead(SENSOR_PIN); //This will read the SENSOR_PIN and get a value of either HIGH or LOW
    
    int sensor_value = analogRead(SENSOR_PIN); //This will read the SENSOR_PIN and get a value from 0 - 1023

    digitalWrite(LED_PIN, HIGH) //This will set the LED_PIN to high (1023)

    analogWrite(LED_PIN, 127) //This will set the LED_PIN to a value of 127 (1)
}

(1) Because the max value is 255 and 127 is half of that, the pulse width modulation will only send a voltage half the time, resulting in the LED being lit with half intesity.

Because the execution of the code is triggered in a loop, variables that needs to be saved between loops needs to be global. This is done simply by placing the variable outside the loop() and setup() functions.

int counter = 0;

void loop(){
    counter++;
}

Serial printing is a way to send messages over the USB port to the terminal on your pc. Serial communication uses a BAUD rate usually it is set to 9600 if the data that is being send is small. Different boards can also communicate together using Serial communication, but that is a bit more advanced.

void setup(){
    Serial.begin(9600); //This sets the serial communication with a baud rate of 9600

}

void loop(){
    Serial.print("HELLO "); //This prints "HELLO " to the terminal
    Serial.println("WORLD"); //This prints "WORLD" to the terminal and adds a new line flag.
}

An arduino has an internal clock that starts running when the arduino starts. It counts in milliseconds. The clock can be accesed using millis()

unsigned long time_since_start = 0;
void loop(){
    time_since_start = millis();
}

cygni-arduino-robot's People

Contributors

christopherbroendholt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.