Skip to content

Instantly share code, notes, and snippets.

@yostane
Last active June 11, 2021 12:52
Show Gist options
  • Select an option

  • Save yostane/a14cfaae77e8ffdfea0d4e30498ac0c6 to your computer and use it in GitHub Desktop.

Select an option

Save yostane/a14cfaae77e8ffdfea0d4e30498ac0c6 to your computer and use it in GitHub Desktop.
const int pin = 13;
const int pin2 = 10;
const int pin3 = 2;
void setup()
{
pinMode(pin, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
}
void switchLeds(int ledOn, int ledOff)
{
digitalWrite(ledOn, HIGH);
digitalWrite(ledOff, LOW);
delay(1000);
}
void loop()
{
switchLeds(pin, pin3);
switchLeds(pin2, pin);
switchLeds(pin3, pin2);
}
#include <stdio.h>
// En c, on ne peut récupérer la taille d'un tableau, il faut passer sa taille avec
int sum(int *items, int n){
printf("sizeof items %ld, nbItems => %ld\n", sizeof(items), sizeof(items) / sizeof(items[0]));
int somme = 0;
for (int i = 0; i < n; i++){
somme += items[i];
}
return somme;
}
int main() {
int n1 = 10;
float n2 = 1.2;
printf("hello %d \n", n1);
int items[] = {1, 2, 3, 4};
int itemsLength = 4;
// ok pour les tableaux statiques
printf("sizeof items %ld, nbItems => %ld\n", sizeof(items), sizeof(items) / sizeof(items[0]));
int *items2 = malloc(6*sizeof(int)); // Memory ALLOCate
printf("sizeof items %ld, nbItems => %ld\n", sizeof(items2), sizeof(items2) / sizeof(items2[0]));
printf("%d \n", sum(items, 4));
// en C, les chaines de caractères ont une caractère en plus à la fin
// sa valeur biniaire c'est 0 -> '\0'
char text[] = "Hello";
for(int i = 0; text[i] != '\0'; i++){
printf("%d - %c \n", i, text[i]);
}
// strlen donne le nombre de chars jusqu'au premier \0
printf("nb chars -> %d", strlen(text));
for(int i = 0; i < strlen(text); i++){
printf("%d - %c \n", i, text[i]);
}
return 0;
}
void setup()
{
Serial.begin(9600);
Serial.println("Hello");
}
void loop()
{
// .available() donne le nombre d'octets en attente dans le port série
Serial.print("available: ");
Serial.println(Serial.available());
if (Serial.available() > 0){
int incomingByte = Serial.read();
Serial.print("Read byte: ");
Serial.println(incomingByte);
Serial.println(incomingByte, HEX);
Serial.println(incomingByte, BIN);
}
delay(1000);
}
// C++ code
//
const int pin = 13;
const int pin2 = 10;
const int pin3 = 2;
// appelé au démarrage (boot) de l'arduino
void setup()
{
// OUTPUT: envoyer du jus (du courant)
pinMode(pin, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
}
//appelé après la fin du setup
//dès qu'elle se termine elle se réexécute
void loop()
{
// HIGH: il y a du courant
digitalWrite(pin, HIGH);
// LOW: pas de courant
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
delay(1000);
digitalWrite(pin, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
delay(1000);
digitalWrite(pin, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment