Lab 3: Controlling the Arduino and Interfacing with Sensor Modules

PART I: Serial.Read()

Objectives and Abstract:

The first portion of the lab involved using Serial.read() functions to control the Arduino. There was an exploratory segment where we played around with Serial.read() to learn how it works. Upon completing this section, the lab involved an exercise where we controlled the Arduino through the Serial Monitor via Serial.read(). I choose to turn the Arduino into a keyboard, using the Piezzo buzzer and using notes A though G (no sharps or flats) in the third octave.

Code

void setup() {

  pinMode(12, OUTPUT);

  Serial.begin(9600);

}

void loop() {

  if (Serial.available()) {

    delay(100);

    char ch = Serial.read();

    if (ch == ‘a’) {

        tone(12, 440, 350);

        delay(100);

    }

    if (ch == ‘b’) {

      tone(12, 493, 350);

      delay(150);

    }

    if (ch == ‘c’) {

      tone(12, 261, 350);

      delay(150);

    }

    if (ch == ‘d’) {

      tone(12, 293, 350);

      delay(150);

    }

    if (ch == ‘e’) {

      tone(12, 329, 350);

      delay(100);

    }

    if (ch == ‘f’) {

      tone(12, 349, 350);

      delay(150);

    }

    if (ch == ‘g’) {

      tone(12, 392, 350);

      delay(150);

  }

  }

}

Setup and Output

The setup and output of the keyboard is provided in the video link below. These are the notes provided into the serial monitor:

CCGGAAG

FFEEDDC

GGFFEED

GGFFEED

CCGGAAG

FFEEDDC

https://drive.google.com/file/d/13-TJOHDtP8JxhguFTGPBElL2ifwsa8xx/view?usp=sharing

The piezzo buzzer was connected directly into the Arduino and ran through 5V and Pin 12.

PART II: ColorPal

Objectives and Abstract:

The objective of the second portion of the lab was to set up the ColorPal and use it to identify colors. Color identification can be particularly relevant to medical devices, especially if done on a more granular level (i.e. analyzing tissue). For this lab, the procedure was more straightforward and did not go quite as into depth. After setting up the ColorPal, we established scaling using a black object and a white object, to mitigate the effects of environmental noise. Within the code, I applied the below standardization formulas – C represents standardized value, U represents the original value, W represents the white scaling, and K represents the black scaling (RGB subscripts are red/green/blue).

Finalized Code:

/==================================================== / Connect ColorPAL SIG signal to Arduino pin 2 and 3 / Add 2K pullup resistor from pins 2 & 3 to +5v / Baud Rate = 9600 kbps / Read signal on 9600 in HEX /====================================================/

include

OldSoftwareSerial Color90(2, 3); // rx = 2, tx = 3

int red;
int grn;
int blu;
int blackRed;
int blackBlue;
int blackGreen;
int whiteRed;
int whiteBlue;
int whiteGreen;

int gotcolor = 0;
int letter;

void setup()
{

Serial.begin(9600); // Start communication with serial port read value
Color90.begin(4800); // Send signal to led to read value

pinMode(2,INPUT); // serial pin out from color pal
pinMode(3,INPUT); // from same serial pin, signal pulls up, sends, pulls down, reads
digitalWrite(2,HIGH); // Enable the pull-up resistor
digitalWrite(3,HIGH); // Enable the pull-up resistor

pinMode(2,OUTPUT); // send signal out
pinMode(3,OUTPUT);
digitalWrite(2,LOW); // turn pin off so pin 3 can go high
digitalWrite(3,LOW);

pinMode(2,INPUT); // Input signal to print
pinMode(3,INPUT);

Serial.println(“Pass 1”);
delay(20);

while( digitalRead(2) != HIGH || digitalRead(3) != HIGH ) {
Serial.println(“In the loop”);
delay(50);
}

Serial.println(“Pass 2”);

pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
delay(100);

pinMode(2,INPUT);
pinMode(3,OUTPUT);
delay(100);

Serial.println(“BLACK”);
delay(5000);
readcolor();
blackRed = red;
blackGreen = grn;
blackBlue = blu;
Serial.println(blackRed);
Serial.println(blackGreen);
Serial.println(blackBlue);
delay(500);

Serial.println(“WHITE”);
delay(5000);
readcolor();
whiteRed = red;
whiteGreen = grn;
whiteBlue = blu;
Serial.println(whiteRed);
Serial.println(whiteGreen);
Serial.println(whiteBlue);
delay(500);

}

// This oscillates back and forth on one wire to turn off led, send signal,
// turn on led, read signal. very fast strobe read – arduino is not capable of
// one wire signal communication over digital ports, so this is a way around
// that over 2 wires communicating with 1 pin on the sensor.
//———————————

void loop()
{
readcolor();
float Cr = (red-blackRed) * 1.0 / ((whiteRed-blackRed) * 1.0);
float Cg = (grn-blackGreen) / ((whiteGreen-blackGreen) * 1.0);
float Cb = (blu-blackBlue) / ((whiteBlue-blackBlue) * 1.0);
Serial.print(“R”);
Serial.print(Cr);
Serial.print(” G”);
Serial.print(Cg);
Serial.print(” B”);
Serial.println(Cb);
if(Cb < 0 && Cg < 0 && Cg < 0){ Serial.println(“black”); } else if(Cb > .9 && Cg > .9 && Cr > .9){
Serial.println(“white”);
} else if(Cg > (Cb + Cr)){
Serial.println(“green”);
} else if (Cb > (Cr + Cg)){
Serial.println(“blue”);
} else if (Cr > (Cb + Cg)){
Serial.println(“red”);
} else if (Cr > Cb && Cg > Cb){
Serial.println(“yellow”);
} else if (Cb > Cr && Cg > Cr){
Serial.println(“cyan”);
} else if (Cr > Cg && Cb > Cg){
Serial.println(“purple”);
} else {
Serial.println(“Unknown”);
}
gotcolor = 0;
delay(5000);

}
void readcolor() { // Reads ColorPAL, putting results in the red,grn,blu variables
char rByte[9];
char dummy[4];

delay(20);
Color90.begin(4800);
Color90.print(“= (00 $ m) !”); // set up loop to continuously send color data
pinMode(3,INPUT);
gotcolor = 0;
while (gotcolor == 0) {
Serial.print(gotcolor);
rByte[0] = Color90.read();
if( rByte[0] == ‘$’ ) {
gotcolor = 1;
for(int i=0; i<9; i++) {
rByte[i] = Color90.read();
Serial.print(rByte[i]);
}
Serial.println(“”);
dummy[0] = rByte[0];
dummy[1] = rByte[1];
dummy[2] = rByte[2];
dummy[3] = 0;

  red = strtol(dummy,NULL,16);

  dummy[0] = rByte[3];
  dummy[1] = rByte[4];
  dummy[2] = rByte[5];

  grn = strtol(dummy,NULL,16);

  dummy[0] = rByte[6];
  dummy[1] = rByte[7];
  dummy[2] = rByte[8];

  blu = strtol(dummy,NULL,16); 
}

}
}

Set Up

Video Results

https://drive.google.com/file/d/1CeIdQExkpIQcp3sQfaidwmCs0gi4XH8O/view?usp=sharing

Conclusion

Lab 3 went pretty smoothly. As you can see from both videos, the outcomes were successful: the ColorPal correctly identified the color of objects placed near it and I was able to use the Arduino and Serial.read() to produce music.

Leave a comment