Back to Parent

#include <Arduino_APDS9960.h>

long randNumber;
int r, g, b;
int relayPin = D2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
    while (!Serial);

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS-9960 sensor.");
  }
  randomSeed(analogRead(0));
  digitalWrite(LEDR,LOW);
  digitalWrite(LEDB,LOW);
}

void loop() {

  // print a random number from 0 to 3
  randNumber = random(20);
  Serial.println(randNumber);
  digitalWrite(relayPin, HIGH);
  //delay(1000);

  // check if a color reading is available
  while (! APDS.colorAvailable()) {
    delay(5);
  }
  //int r, g, b;
    // read the color
    APDS.readColor(r, g, b);
  
  //morse Code
  if (randNumber == 0){
    //beginning signal
    digitalWrite(relayPin, LOW);
    delay(3000);

    // read the color
    APDS.readColor(r, g, b);
    

      do{
        digitalWrite(LEDR,HIGH);
        digitalWrite(LEDB,HIGH);
        morseRed();
        //delay(1000);

        while (! APDS.colorAvailable()) {
          delay(5);
        }

        APDS.readColor(r, g, b);
        Serial.print("Current r: ");
        Serial.println(r-130);
        Serial.print("Current g: ");
        Serial.println(g);
        Serial.print("Current b: ");
        Serial.println(b);
        Serial.println();
        }while (r-130 < g && r-130 < b);
  
      
  }
  else if(randNumber ==1){

  //beginning signal
      digitalWrite(relayPin, LOW);
      delay(3000);

      // read the color
      APDS.readColor(r, g, b);
      

        do{
        digitalWrite(LEDR,HIGH);
        digitalWrite(LEDB,HIGH);
          morseGreen();
          //delay(1000);

          while (! APDS.colorAvailable()) {
            delay(5);
          }
          APDS.readColor(r, g, b);
          Serial.print("Current r: ");
          Serial.println(r-130);
          Serial.print("Current g: ");
          Serial.println(g);
          Serial.print("Current b: ");
          Serial.println(b);
          Serial.println();
          }while (g < r-130 && g < b);
    }else if(randNumber ==2){
    //  morseBlue();  
    // begining signal
      digitalWrite(relayPin, LOW);
      delay(3000);

      // read the color
      APDS.readColor(r, g, b);
      

        do{
        digitalWrite(LEDR,HIGH);
        digitalWrite(LEDB,HIGH);
          morseBlue();
          //delay(1000);

          while (! APDS.colorAvailable()) {
            delay(5);
          }
          APDS.readColor(r, g, b);
          Serial.print("Current r: ");
          Serial.println(r-130);
          Serial.print("Current g: ");
          Serial.println(g);
          Serial.print("Current b: ");
          Serial.println(b);
          Serial.println();
          }while (b < g && b < r-130);

    }else{
      //Serial.println(randNumber);
      //delay(1000);

    }
  

  Serial.print("r = ");
  Serial.println(r-100);
  Serial.print("g = ");
  Serial.println(g);
  Serial.print("b = ");
  Serial.println(b);
  Serial.println();

  // wait a bit before reading again
  delay(1000);


}


void morseDot(){
  //dot
  digitalWrite(LEDR,LOW);
  digitalWrite( relayPin, HIGH );
  delay(500);
  digitalWrite(LEDR,HIGH);
  digitalWrite( relayPin, LOW );
  delay(1500);
}

void morseDash(){
   //dash
  digitalWrite(LEDR,LOW);
  digitalWrite( relayPin, HIGH );
  delay(1500);
  digitalWrite(LEDR,HIGH);
  digitalWrite( relayPin, LOW );
  delay(1500); 
}

void morseRed(){
  //R: dot-dash-dot
  Serial.println("morse RED");
  morseDot();
  morseDash();
  morseDot();
 /* 
  //E: dot
  morseDot();
  
  //D: dash-dot-dot
  morseDash();
  morseDot();
  morseDot();
 */
}

void morseGreen(){

  //G: dash-dash-dot
  morseDash();
  morseDash();
  morseDot();

}

void morseBlue(){

  //B: dash-dot-dot-dot
  morseDash();
  morseDot();
  morseDot();
  morseDot();

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0