Let's play with arduino part 2

Arduino codes for different components :

/*
In This code we will try to generate words and letters to Morse code ! using both of light and sound!
*/
char* morse[] = {".-","-...","-.-.","-..",".","..-.","--.","....", // 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

int one_unit = 500;     // half-second for the "." code. and between letters.
int three_unit = 1500;  // secand and helf for the "-" code.
int seven_unit = 3500;  // three second and helf for the space between words.
char ch;       // for serial.read()
int buzzer = 10;  //buzzer's Pin
int led = 2;  //LED's Pin

void setup() {
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}

void on(){ //function to turon on LED and buzzer
  analogWrite(buzzer, 115);
  digitalWrite(led, HIGH);
}

void off(){ //function to turn off both of LED and buzzer
  analogWrite(buzzer, 0);
  digitalWrite(led, LOW);
}

void say(char m){
  if (m == '.'){
    on();
    delay(one_unit);
  }else if (m == '-'){
    on();
    delay(three_unit);
  }
  off();
  delay(three_unit);
}

void take(char* seq){
  int i=0;
  while(seq[i] != '\0'){
    say(seq[i]);
    i++;
  }
}

void loop() {
  if (Serial.available()>0){
    ch = Serial.read(); //this will take a char easch time !

    if (ch >= 'a' && ch <= 'z'){
      take(morse[ch-'a']);
    }else if (ch >= 'A' && ch <= 'Z'){
      take(morse[ch-'A']);
    }else if(ch == ' '){ //this for the space!
      off();
      delay(seven_unit);
    }else{ // in case of Error!
      Serial.print(ch);
      Serial.println(" = Error");
    }
  }
}

/*
This code use a RGB LED and button to switch to 9 different color, every time when you pressed the button.
*/
int red = 9;
int green = 10;
int blue = 11;
int button = 8;
int led=1;
void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(button, INPUT);
}

void turnOn(String light){
  if (light == "red"){
    analogWrite(red, 255);
    analogWrite(green, 0);
    analogWrite(blue, 0);
  }else if (light == "green"){
    analogWrite(red, 0);
    analogWrite(green, 255);
    analogWrite(blue, 0);
  }else if(light == "blue"){
    analogWrite(red, 0);
    analogWrite(green, 0);
    analogWrite(blue, 255);
  }else if(light == "yellow"){
    analogWrite(red, 255);
    analogWrite(green, 255);
    analogWrite(blue, 0);
  }else if(light == "purple"){
    analogWrite(red, 128);
    analogWrite(green, 0);
    analogWrite(blue, 255);
  }else if (light == "aqua"){
    analogWrite(red, 0);
    analogWrite(green, 255);
    analogWrite(blue, 255);
    }else if(light == "white"){
      analogWrite(red, 255);
      analogWrite(green, 255);
      analogWrite(blue, 255);
    }else if(light == "pink"){
      analogWrite(red, 175);
      analogWrite(green, 75);
      analogWrite(blue, 148);
    }else if(light == "orange"){
      analogWrite(red, 237);
      analogWrite(green, 120);
      analogWrite(blue, 6);
    }else if(light == "off"){
      analogWrite(red, 0);
      analogWrite(green, 0);
      analogWrite(blue, 0);
    }
}

void loop() {
  if (digitalRead(button) == HIGH && (led == 1)){
      turnOn("red");
      led = 2;
      delay(300);
  }
  if (digitalRead(button) == HIGH && (led == 2)){
      turnOn("green");
      led = 3;
      delay(300);
  }
 if (digitalRead(button) == HIGH && (led == 3) ){
      turnOn("blue");
      led = 4;
      delay(300);
  }
  if (digitalRead(button) == HIGH && (led == 4)){
      turnOn("yellow");
      led = 5;
      delay(300);
  }
  if (digitalRead(button) == HIGH && (led == 5)){
      turnOn("purple");
      led = 6;
      delay(300);
  }
 if (digitalRead(button) == HIGH && (led == 6) ){
      turnOn("aqua");
      led = 7;
      delay(300);
  }
 if (digitalRead(button) == HIGH && (led == 7)){
      turnOn("white");
      led = 8;
      delay(300);
  }
 if (digitalRead(button) == HIGH && (led == 8)){
       turnOn("pink");
       led = 9;
       delay(300);
  }
 if (digitalRead(button) == HIGH && (led == 9)){
       turnOn("orange");
       led = 0;
       delay(300);
  }
 if (digitalRead(button) == HIGH && (led == 0)){
       turnOn("off");
       led = 1;
       delay(300);
  }
}

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.print("Ghassen ");
  lcd.setCursor(0, 1);
  lcd.print("ben taher");
}

void loop()
{
  // Do nothing here...
}

Comments