---
# System prepended metadata

title: Arduino C Introductory Exercises

---

## Barduino pinout

If you have version 4.0.2+, you will find the onboard device pin numbers in the back of the board.

In any case, [they are detailed in the documentation](https://fablabbcn-projects.gitlab.io/electronics/barduino-docs/GettingStarted/pinout/). For example, to turn on and on the onboard LED, you will need to do:

```arduino
const int ledPin = 48;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}
```


### Exercise: Blink again

Rewrite the above sketch to make the LED blink 10 times per second.

### Exercise: Push it 

See how fast you can make the LED blink before you perceive it as a continuous light.

### Exercise: button up

Read the state of the "BOOT" button (connected to pin 0) and show it to screen using `Serial.println`

* _Hint_: `pinMode`
* _Hint_: What [type of variable](https://docs.arduino.cc/language-reference/en/variables/data-types/boolean/) should you use here?
* _Hint_: `Serial.begin`
* _Hint_: Have you set "USB CDC on Boot" to "Enabled" in the "Tools" menu?

<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
const int ledPin = 48;
const int buttonPin = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  
  Serial.begin(115200);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(100);
  Serial.println(digitalRead(buttonPin));
}

```
</details>

### Exercise: excitable Barduino

Now connect both actions. Make the Arduino blink slowly (once per second) when the button is not pressed and fast (10 times per second) when the button is pressed.

* _Hint_: It's not strictly necessary, but the most natural way to write this is [with an `if` statement](https://docs.arduino.cc/built-in-examples/control-structures/ifStatementConditional/)

<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
const int ledPin = 48;
const int buttonPin = 0;

int delayTime = 1000;

  void
  setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  Serial.begin(115200);
}

void loop() {
  boolean unpressed = digitalRead(buttonPin);
  Serial.println(unpressed);

  if (unpressed) {
    delayTime = 1000;
  } else {
    delayTime = 50;
  }

  digitalWrite(ledPin, HIGH);
  delay(delayTime);
  digitalWrite(ledPin, LOW);
  delay(delayTime);
}

```
</details>


### Exercise: chirpy Barduino

[The `tone` function](https://docs.arduino.cc/language-reference/en/functions/advanced-io/tone/) generates a square wave that we can use to make a buzzer sound in a specific frequency. Barduino has one connected to pin 46.

Example: one tone at 440Hz (middle A) for 200 milliseconds on startup.
 
```arduino
int buzzerPin = 46;

void setup() {
  tone(buzzerPin, 440, 200);
}

void loop() {
}
```


Make Barduino beep! Make it sing a "startup" tone, in which we have three notes in a rising scale.


<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
int buzzerPin = 46;

void setup() {
  tone(buzzerPin, 440, 200);
  tone(buzzerPin, 880, 200);
  tone(buzzerPin, 1760, 200);
}

void loop() {
}

```
</details>



### Exercise: mix and match

Integrate both the chirpy Barduino result and the Excitable Barduino sketches so that you keep the functionality of both.

<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
const int buttonPin = 0;
const int ledPin = 48;
const int buzzerPin = 46;

int delayTime = 1000;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  tone(buzzerPin, 440, 200);
  tone(buzzerPin, 880, 200);
  tone(buzzerPin, 1760, 200);

  Serial.begin(115200);
}

void loop() {
  boolean unpressed = digitalRead(buttonPin);
  Serial.println(unpressed);

  if (unpressed) {
    delayTime = 1000;
  } else {
    delayTime = 50;
  }

  digitalWrite(ledPin, HIGH);
  delay(delayTime);
  digitalWrite(ledPin, LOW);
  delay(delayTime);
}

```
</details>

### Exercise: all together now

Extend the previous sketch so that Barduino beeps once per light pulse, at 2000Hz when the button is pressed and 200Hz otherwise.

<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
const int buttonPin = 0;
const int ledPin = 48;
const int buzzerPin = 46;

int delayTime = 1000;
int toneFrequency = 440;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  tone(buzzerPin, toneFrequency, 200);
  tone(buzzerPin, 2 * toneFrequency, 200);
  tone(buzzerPin, 4 * toneFrequency, 200);

  Serial.begin(115200);
}

void loop() {
  boolean unpressed = digitalRead(buttonPin);
  Serial.println(toneFrequency);

  if (unpressed) {
    delayTime = 1000;
    toneFrequency = 200;
  } else {
    delayTime = 50;
    toneFrequency = 2000;
  }

  digitalWrite(ledPin, HIGH);
  tone(buzzerPin, toneFrequency, delayTime);
  delay(delayTime);

  digitalWrite(ledPin, LOW);
  delay(delayTime);
}

```
</details>

### Exercise: Shine a light

Barduino has a light sensor on pin 3. Measure ambient light, show the reading to screen, and figure out how high and how low the values go.

* _Hint_: You will need to use the [`analogRead()`](https://www.arduino.cc/reference/tr/language/functions/analog-io/analogread/) function.

<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
const int lightSensorPin = 3;

void setup() {
  pinMode(lightSensorPin, INPUT);

  Serial.begin(115200);
}

void loop() {
  Serial.println(analogRead(lightSensorPin));  // Will print 0 to 4095 depending on ambient light.
}
```
</details>


### Exercise: Peek a Boo

Más difícil todavía! Now, integrate the light reading, the blinking and the beeping. We want a whiny board, one that beeps higher and blinks faster the more ambient light there is.

<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
const int buttonPin = 0;
const int lightSensorPin = 3;
const int ledPin = 48;
const int buzzerPin = 46;  // Barduino 4.0.2+

int delayTime = 1000;
int toneFrequency = 440;
int lightReading = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(lightSensorPin, INPUT);

  tone(buzzerPin, toneFrequency, 200);
  tone(buzzerPin, 2 * toneFrequency, 200);
  tone(buzzerPin, 4 * toneFrequency, 200);

  Serial.begin(115200);
}

void loop() {
  boolean unpressed = digitalRead(buttonPin);
  lightReading = analogRead(lightSensorPin);

  delayTime = (4096 - lightReading) / 4;  // Make it faster (less delay) the more light there is. Never delay more than 1023 milliseconds.
  toneFrequency = lightReading / 2;     // Make it higher the more light there is. Never higher than 2048Hz.

  // This is a little trick: if you format your prints like this, you can 
  // observe the values of the variables as graphs through the Arduino IDE's
  // Serial plotter (the ECG-like symbol on the top right).
  Serial.print("delayTime:");
  Serial.print(delayTime);
  Serial.print(" lightReading:");
  Serial.println(lightReading);

  digitalWrite(ledPin, HIGH);

  tone(buzzerPin, toneFrequency, delayTime);

  delay(delayTime);

  digitalWrite(ledPin, LOW);
  delay(delayTime);
}


```
</details>


### Exercise: Failsafe

That's a racket! If you try this on a bus, you might get kicked out. Introduce a failsafe so that holding down the "BOOT" button silences the beeping.


<details>
<summary>
Solution: click to show.
</summary>
    
```arduino
const int buttonPin = 0;
const int lightSensorPin = 3;
const int ledPin = 48;
const int buzzerPin = 46;  // Barduino 4.0.2+

int delayTime = 1000;
int toneFrequency = 440;
int lightReading = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(lightSensorPin, INPUT);

  tone(buzzerPin, toneFrequency, 200);
  tone(buzzerPin, 2 * toneFrequency, 200);
  tone(buzzerPin, 4 * toneFrequency, 200);

  Serial.begin(115200);
}

void loop() {
  boolean unpressed = digitalRead(buttonPin);
  lightReading = analogRead(lightSensorPin);

  delayTime = (4096 - lightReading) / 4;  // Make it faster (less delay) the more light there is. Never delay more than 1023 milliseconds.
  toneFrequency = lightReading / 2;     // Make it higher the more light there is. Never higher than 2048Hz.

  // This is a little trick: if you format your prints like this, you can 
  // observe the values of the variables as graphs through the Arduino IDE's
  // Serial plotter (the ECG-like symbol on the top right).
  Serial.print("delayTime:");
  Serial.print(delayTime);
  Serial.print(" lightReading:");
  Serial.println(lightReading);

  digitalWrite(ledPin, HIGH);

  if (unpressed) {
    tone(buzzerPin, toneFrequency, delayTime);
  }

  delay(delayTime);

  digitalWrite(ledPin, LOW);
  delay(delayTime);
}
```
</details>

## Further projects

You can find more projects to continue from here in many websites. One option that's old but gold is the [Arduino projects book](https://www.uio.no/studier/emner/matnat/ifi/IN1060/v21/arduino/arduino-projects-book.pdf) (PDF, 7.6MB).