Morse Code:
const int DOT = 0;
const int DASH = 1;
const int SPACE = 3;
const int STOP = 2;
int counter = 0;
const int DOT_DURATION = 500;
const int DASH_DURATION = DOT_DURATION * 3;
const int sleep = 250;
const int indexes_length = 27;
const int SPACE_DURATION = DASH_DURATION * 2;
int letters[indexes_length][5] = {
{DOT,DASH, STOP}, // a
{DASH, DOT, DOT, DOT, STOP}, // b
{DASH, DOT, DASH, DOT, STOP}, // c
{DASH, DOT, DOT, STOP}, // d
{DOT, STOP}, // e
{DOT, DOT, DASH, DOT, STOP}, // f
{DASH, DASH, DOT, STOP}, // g
{DOT, DOT, DOT, DOT, STOP},// h
{DOT, DOT, STOP}, //I
{DOT, DASH, DASH, DASH, STOP}, // j
{DASH, DOT, DASH, STOP}, // k
{DOT, DASH, DOT, DOT, STOP}, // l
{DASH, DASH, STOP}, // m
{DASH, DOT, STOP}, // n
{DASH, DASH, DASH, STOP}, // o
{DOT, DASH, DASH, DOT, STOP}, // p
{DASH, DASH, DOT, DASH, STOP}, // q
{DOT, DASH, DOT, STOP}, // r
{DOT, DOT, DOT, STOP}, // s
{DASH, STOP}, // t
{DOT, DOT, DASH, STOP}, // u
{DOT, DOT, DOT, DASH, STOP}, // v
{DOT, DASH, DASH, STOP}, // w
{DASH, DOT, DOT, DASH, STOP}, // x
{DASH, DOT, DASH, DASH, STOP}, // y
{DASH, DASH, DOT, DOT, STOP}, // z
{SPACE, STOP}, // space
};
char indexes[indexes_length] = {
‘a’,
‘b’,
‘c’,
‘d’,
‘e’,
‘f’,
‘g’,
‘h’,
‘i’,
‘j’,
‘k’,
‘l’,
‘m’,
‘n’,
‘o’,
‘p’,
‘q’,
‘r’,
‘s’,
‘t’,
‘u’,
‘v’,
‘w’,
‘x’,
‘y’,
‘z’,
‘ ‘,
};
char message[14] = {
‘i’,
‘ ‘,
‘l’,
‘o’,
‘v’,
‘e’,
‘ ‘,
‘y’,
‘o’,
‘u’,
‘ ‘,
‘m’,
‘o’,
‘m’,
};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
for(int i = 0; i <= 14; i++)
{
// h, e, l, l, o…
for (int j = 0; j <= indexes_length; j++)
{
// Lookup character index
if (indexes[j] == message[i])
{
for (int k = 0; k <= 5; k++)
{
if (letters[j][k] == STOP)
{
break;
}
blink_led(letters[j][k]);
}
break;
}
}
};
}
void loop() {
// for each character in the message string, e.g. “h” and then “e”, etc…
// look up the index of the character in the alphabet
// then in the letters array, access the dash/dot stuff.
// for each signal in the dash/dot stuff, call blink_led(signal)
// if the signal is “STOP”, then abort the loop
}
void blink_led(int signal)
{
if (signal == DOT)
{
digitalWrite(4, HIGH);
delay(DOT_DURATION);
digitalWrite(4, LOW);
delay(sleep);
}
else if (signal == DASH)
{
digitalWrite(5, HIGH);
delay(DASH_DURATION);
digitalWrite(5, LOW);
delay(sleep);
}
else if (signal == SPACE)
{
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(SPACE_DURATION);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(SPACE_DURATION);
}
}
Remote Controlled Car:
const int a_pwm_pin = 26;
const int a_dir1_pin = 31;
const int a_dir2_pin = 33;
// Pins for motor B
const int b_pwm_pin = 5;
const int b_dir1_pin = 35;
const int b_dir2_pin = 37;
// pins for motor C
const int c_pwm_pin = 9;
const int c_dir1_pin = 8;
const int c_dir2_pin = 7;
// pins for motor D
const int d_pwm_pin = 10;
const int d_dir1_pin =12;
const int d_dir2_pin =11;
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println(“Ultrasonic Sensor HC-SR04 Test”); // print some text in Serial Monitor
Serial.println(“with Arduino UNO R3”);
// Set up pins for motor A
pinMode(a_pwm_pin, OUTPUT);
pinMode(a_dir1_pin, OUTPUT);
pinMode(a_dir2_pin, OUTPUT);
// Set up pins for motor B
pinMode(b_pwm_pin, OUTPUT);
pinMode(b_dir1_pin, OUTPUT);
pinMode(b_dir2_pin, OUTPUT);
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print(“Distance: “);
Serial.print(distance);
Serial.println(” cm”);
if(distance < 150)
{
Serial.println(“less than 50”);
stop();
stop_front();
}
else
{
Serial.println(“not”);
forward(255);
forward_front(255);
}
}
void forward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A forward
digitalWrite(a_dir1_pin, HIGH);
digitalWrite(a_dir2_pin, LOW);
analogWrite(a_pwm_pin, speed);
// Motor B forward
digitalWrite(b_dir1_pin, HIGH);
digitalWrite(b_dir2_pin, LOW);
analogWrite(b_pwm_pin, speed);
}
// Drive backward
void backward(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A backward
digitalWrite(a_dir1_pin, LOW);
digitalWrite(a_dir2_pin, HIGH);
analogWrite(a_pwm_pin, speed);
// Motor B backward
digitalWrite(b_dir1_pin, LOW);
digitalWrite(b_dir2_pin, HIGH);
analogWrite(b_pwm_pin, speed);
}
// Turn left
void left(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A backward
digitalWrite(a_dir1_pin, LOW);
digitalWrite(a_dir2_pin, HIGH);
analogWrite(a_pwm_pin, speed);
// Motor B forward
digitalWrite(b_dir1_pin, HIGH);
digitalWrite(b_dir2_pin, LOW);
analogWrite(b_pwm_pin, speed);
}
// Turn right
void right(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A forward
digitalWrite(a_dir1_pin, HIGH);
digitalWrite(a_dir2_pin, LOW);
analogWrite(a_pwm_pin, speed);
// Motor B backward
digitalWrite(b_dir1_pin, LOW);
digitalWrite(b_dir2_pin, HIGH);
analogWrite(b_pwm_pin, speed);
}
// Stop
void stop() {
// Motor A brake
digitalWrite(a_dir1_pin, LOW);
digitalWrite(a_dir2_pin, LOW);
analogWrite(a_pwm_pin, 0);
// Motor B brake
digitalWrite(b_dir1_pin, LOW);
digitalWrite(b_dir2_pin, LOW);
analogWrite(b_pwm_pin, 0);
}
void forward_front(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A forward
digitalWrite(c_dir1_pin, HIGH);
digitalWrite(c_dir2_pin, LOW);
analogWrite(c_pwm_pin, speed);
// Motor B forward
digitalWrite(d_dir1_pin, HIGH);
digitalWrite(d_dir2_pin, LOW);
analogWrite(d_pwm_pin, speed);
}
// Drive backward
void backward_front(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A backward
digitalWrite(c_dir1_pin, LOW);
digitalWrite(c_dir2_pin, HIGH);
analogWrite(c_pwm_pin, speed);
// Motor B backward
digitalWrite(d_dir1_pin, LOW);
digitalWrite(d_dir2_pin, HIGH);
analogWrite(d_pwm_pin, speed);
}
// Turn left
void left_front(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A backward
digitalWrite(c_dir1_pin, LOW);
digitalWrite(c_dir2_pin, HIGH);
analogWrite(c_pwm_pin, speed);
// Motor B forward
digitalWrite(d_dir1_pin, HIGH);
digitalWrite(d_dir2_pin, LOW);
analogWrite(d_pwm_pin, speed);
}
// Turn right
void right_front(int speed) {
// Check to make sure speed is 0-255
if ( speed < 0 ) {
speed = 0;
}
if ( speed > 255 ) {
speed = 255;
}
// Motor A forward
digitalWrite(c_dir1_pin, HIGH);
digitalWrite(c_dir2_pin, LOW);
analogWrite(c_pwm_pin, speed);
// Motor B backward
digitalWrite(d_dir1_pin, LOW);
digitalWrite(d_dir2_pin, HIGH);
analogWrite(d_pwm_pin, speed);
}
// Stop
void stop_front() {
// Motor A brake
digitalWrite(c_dir1_pin, LOW);
digitalWrite(c_dir2_pin, LOW);
analogWrite(c_pwm_pin, 0);
// Motor B brake
digitalWrite(d_dir1_pin, LOW);
digitalWrite(d_dir2_pin, LOW);
analogWrite(d_pwm_pin, 0);
}
LEDs back in forth cool pattern:
int pin_before = 0;
int pin_after = 0;
const int Led_list[6] = {
3,
5,
6,
9,
10,
11,
};
int status[6] = {
0,
0,
0,
0,
0,
0,
};
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
for (int i = 0; i < 6; i++)
{
status[i] = 1;
turn_on(Led_list[i]);
// loop through status array to see if led is on to make dim lights
for (int k = 0; k < 6; k++)
{
if (status[k] == 1 )
{
// check to see if led before current pin
if (k – 1 >= 0) {
pin_before = Led_list[k – 1];
}
else{
pin_before = 0;
}
// check to see if led after current pin
if (k + 1 < 6) {
pin_after = Led_list[k + 1];
}
else
{
pin_after = 0;
}
dim_leds(pin_after, pin_before);
}
}
status[i] = 0;
turn_off(Led_list[i]);
}
// start after 5 because 6 kept blinking(twice)
for(int i = 4; i >= 1; i–)
{
status[i] = 1;
turn_on(Led_list[i]);
for(int k = 5; k >= 0; k–)
{
if(status[k] == 1)
{
if (k + 1 <= 6)
{
pin_after = Led_list[k + 1];
}
if(k – 1 >= 0)
{
pin_before = Led_list[k – 1];
}
else{
pin_after = 0;
}
dim_leds(pin_after, pin_before);
}
}
status[i] = 0;
turn_off(Led_list[i]);
}
}
void turn_on(int led)
{
Serial.print(“On pin #”);
Serial.println(led);
digitalWrite(led, HIGH);
}
void dim_leds(int pin1, int pin2)
{
Serial.print(“Dimming pin #”);
Serial.print(pin1);
Serial.print(” “);
Serial.print(pin2);
Serial.println(” “);
analogWrite(pin1, HIGH);
analogWrite(pin2, HIGH);
delay(9);
analogWrite(pin1, LOW);
analogWrite(pin2, LOW);
delay(1);
}
void turn_off(int led)
{
digitalWrite(led, LOW);
}