Part 8 - int

Today we are going to work with the int data type which are nothing more than whole numbers. They can be signed or unsigned as well.

Let's work with a simple example. 0x04_int.c as follows.

#include <stdio.h>
#include "pico/stdlib.h"

int main() 
{
  stdio_init_all();

  while(1) 
  {
    int x = 40; 

    printf("%d\n", x); 

    sleep_ms(1000);
  }

  return 0;
}

Here we simply have our standard IO function followed by our infinite loop. We simply assign 40 to the int data type x and print it using the %d format modifier and sleep for 1 second.

Let's make a new dir 0x04_int and add our CMakeLists.txt file in it.

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(test_project C CXX ASM)
set(CMAKE_C_STANDARD 11) 
set(CMAKE_CXX_STANDARD 17) 
pico_sdk_init()

add_executable(0x04_int
  0x04_int.c
)

pico_enable_stdio_usb(0x04_int 1)

pico_add_extra_outputs(0x04_int)

target_link_libraries(0x04_int pico_stdlib)

Next we need to copy the pico_sdk_import.cmake file from the external folder in the pico-sdk installation to the 0x04_int project folder.

cp ../pico-sdk/external/pico_sdk_import.cmake .

Finally we are ready to build.

mkdir build
cd build
export PICO_SDK_PATH=../../pico-sdk
cmake ..
make

Then simply copy the .uf2 file to the drive.

cp 0x04_int.uf2 /Volumes/RPI-RP2

Then we need to locate the USB drive so you can do the following.

ls /dev/tty.

Press tab to find the drive and then in my case I will use screen to connect.

screen /dev/tty.usbmodem0000000000001

You should see a an 40 being printed every second.

40
40
40
40
40
40
40
40
40
40
40
40

In our next lesson we will debug.

results matching ""

    No results matching ""