Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lab1/button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#define BUTTON_DEBOUNCE_DELAY 50

class Button
{
public:
Button(int _pin)
{
pin = _pin;
state = LOW;
debounceState = LOW;

pinMode(pin, INPUT_PULLUP);
}

bool wasPressed()
{
bool result = false;
int lastSeen = state;
readButtonWithDebounce();

if (lastSeen == HIGH && state == LOW)
result = true;

return result;
}

void readButtonWithDebounce()
{
int current = digitalRead(pin);

if (current != debounceState)
debounceLastMs = millis();

if ((millis() - debounceLastMs) > BUTTON_DEBOUNCE_DELAY)
state = current;

debounceState = current;
}

private:
int pin;
int state;
int debounceState;
unsigned long debounceLastMs;
};
74 changes: 74 additions & 0 deletions lab1/buzzer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

#define BUZZER_NOTE_DURATION 100

class Buzzer
{
public:
Buzzer(int _pin)
{
pin = _pin;
pinMode(pin, OUTPUT);

isEnabled = false;
currentNote = 0;
noteStartedMs = 0;

notes = 0;
durations = 0;
melodyLength = 0;
}

void turnSoundOn()
{
isEnabled = true;
currentNote = 0;
noteStartedMs = 0;
}

void turnSoundOff()
{
isEnabled = false;
currentNote = 0;
noteStartedMs = 0;
noTone(pin);
}

void setMelody(int _notes[], double _durations[], int _melodyLength)
{
notes = _notes;
durations = _durations;
melodyLength = _melodyLength;
}

void playSound()
{
if (!isEnabled)
return;

unsigned long duration = round(BUZZER_NOTE_DURATION*durations[currentNote]);
if ((millis() - noteStartedMs) > duration)
{
int note = notes[currentNote];

if (note == NOTE_SILENCE)
noTone(pin);
else
tone(pin, notes[currentNote]);

noteStartedMs = millis();
currentNote = (currentNote + 1)%melodyLength;
}
}

private:
int pin;
bool isEnabled;

int currentNote;
unsigned long noteStartedMs;

int* notes;
double* durations;
int melodyLength;
};
104 changes: 53 additions & 51 deletions lab1/lab1.ino
Original file line number Diff line number Diff line change
@@ -1,66 +1,68 @@
#include <Arduino.h>
#include <MD_TCS230.h>
#include "pitches.h"
#include "button.h"

#define S0_OUT 2
#define S1_OUT 3
#define S2_OUT 4
#define S3_OUT 5
#define PIN_BUTTON_RedUp 4
#define PIN_BUTTON_RedDown 5
#define PIN_BUTTON_GreenUp 6
#define PIN_BUTTON_GreenDown 7
#define PIN_BUTTON_BlueUp 8
#define PIN_BUTTON_BlueDown 9

#define R_OUT 6
#define G_OUT 7
#define B_OUT 8
#define R_OUT 1
#define G_OUT 2
#define B_OUT 3

MD_TCS230 colorSensor(S2_OUT, S3_OUT, S0_OUT, S1_OUT);
int r = 255
int g = 255
int b = 255

void setup()
void set_rgb_led(int r, int g, int b)
{
Serial.begin(115200);
Serial.println("Started!");

sensorData whiteCalibration;
whiteCalibration.value[TCS230_RGB_R] = 0;
whiteCalibration.value[TCS230_RGB_G] = 0;
whiteCalibration.value[TCS230_RGB_B] = 0;

sensorData blackCalibration;
blackCalibration.value[TCS230_RGB_R] = 0;
blackCalibration.value[TCS230_RGB_G] = 0;
blackCalibration.value[TCS230_RGB_B] = 0;
analogWrite(R_OUT, r);
analogWrite(G_OUT, g);
analogWrite(B_OUT, b);
}

colorSensor.begin();
colorSensor.setDarkCal(&blackCalibration);
colorSensor.setWhiteCal(&whiteCalibration);
Button buttonRU(PIN_BUTTON_RedUp);
Button buttonRD(PIN_BUTTON_RedDown);
Button buttonGU(PIN_BUTTON_GreenUp);
Button buttonGD(PIN_BUTTON_GreenDown);
Button buttonBU(PIN_BUTTON_BlueUp);
Button buttonBG(PIN_BUTTON_BlueDown);

void setup()
{
pinMode(R_OUT, OUTPUT);
pinMode(G_OUT, OUTPUT);
pinMode(B_OUT, OUTPUT);
}

void loop()
{
colorData rgb;
colorSensor.read();

while (!colorSensor.available());

colorSensor.getRGB(&rgb);
print_rgb(rgb);
set_rgb_led(rgb);
}

void print_rgb(colorData rgb)
{
Serial.print(rgb.value[TCS230_RGB_R]);
Serial.print(" ");
Serial.print(rgb.value[TCS230_RGB_G]);
Serial.print(" ");
Serial.print(rgb.value[TCS230_RGB_B]);
Serial.println();
}

void set_rgb_led(colorData rgb)
void loop()
{
analogWrite(R_OUT, 255 - rgb.value[TCS230_RGB_R]);
analogWrite(G_OUT, 255 - rgb.value[TCS230_RGB_G]);
analogWrite(B_OUT, 255 - rgb.value[TCS230_RGB_B]);
set_rgb_led(r, g, b)
if (buttonRU.wasPressed() && r <= 255)
{
r += 5
}
if (buttonRD.wasPressed() && r >= 0)
{
r -= 5
}
if (buttonGU.wasPressed() && g <= 0)
{
g += 5
}
if (buttonGD.wasPressed() && g >= 0)
{
g -= 5
}
if (buttonBU.wasPressed() && b <= 255)
{
b += 5
}
if (buttonBD.wasPressed() && b >= 0)
{
b -= 5
}
}
95 changes: 95 additions & 0 deletions lab1/pitches.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*************************************************
* Public Constants
*************************************************/

#define NOTE_SILENCE 0

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978