Skip to content

January 31, 2012

The Computer Scientist vs. Electrical Engineer

Photo by the Arduino Team

As a computer science student I have become quite efficient at cranking out code for various things, as well as learning how to teach others to do the same.  Well today was my first electrical engineering lab, and I noticed that many of them were not proficient in coding (not that they are necessarily expected to be) even though they generally had at least one coding class in the university.  This was the first time in the class where I felt as a computer science student in the electrical engineering department, where I felt that I had the upper hand.  Today’s lab was quite simple, it had various problems which had to be implemented on the arduino ending with the following one.  Essentially write an arduino program that can take an input from the serial monitor of a time in seconds, then have it so that the arduino turns the LED on pin 9 on for that long and off for that long.  The problem originally wanted it to only work for a single digit (between 1 and 9), however I felt that was too easy after I wrote that in about 5 minutes. 

/*
Reads a blink rate in seconds from the serial monitor
adjusting the rate as necessary.

Author: Sean Smith
date: 1/30/12
*/

int blinkRate = 1000;  //blink rate which is 1 second by default

void setup() {
  //starts the serial port and sets the pin mode
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  if(Serial.available() > 0){
    int input = Serial.read();
    if(input > 48 && input < 72){
      int rate = input - 48;
      blinkRate = rate*1000;
      Serial.println("Updated blink rate to once every " + String(rate) + " seconds");
    }else if(input == 48){
      Serial.println("Error: cannot have a rate of less than 1");
    }else{
      Serial.println("Error: cannont have a non-didgit blink rate");
    }
  }
  digitalWrite(9, HIGH);  //light on
  delay(blinkRate);        //wait for so long
  digitalWrite(9, LOW);   //light off
  delay(blinkRate);        //wait for sop long
}

About 10 minutes later I wrote this, which will work for multi-digit integers (say 10 or 11), which my professor felt there was no simple way to implement for our class.

/*
Reads a blink rate in seconds from the serial monitor
adjusting the rate as necessary.

Author: Sean Smith
date: 1/30/12
*/

int blinkRate = 1000;  //blink rate which is 1 second by default

/*
This is used to initialize the arduino and start up the serial
monitor

@Author: Sean Smith
*/
void setup() {
  //starts the serial port and sets the pin mode
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

/*
This is the loop where all the work is done reading from the serial
and adjusting the blink rate as necessary.  An interesting bug (maybe)
is that the blink rate goes back to the standard 1 second if the
serial monitor is closed

@Author: Sean Smith
*/
void loop() {
  String newRate = "";  //what we will use for the new rate
  if(Serial.available() > 0){  //if theres stuff on the serial read it
    while(Serial.available() > 0){  //loop thru all the numbers on the serial
      int input = Serial.read();
      if(input > 47 && input < 58){  //if it is between 0 and 9
        newRate += String(input-48);  //add it to the end of the new rate
      }
    }
    blinkRate = newRate.toInt()*1000;
    if(blinkRate < 1000)  //if it's less than 1 second, make it 1 second
      blinkRate = 1000;
    Serial.println("The new blink rate is once every " + String(blinkRate/1000) + " seconds");
  }
  digitalWrite(9, HIGH);  //light on
  delay(blinkRate);        //wait for so long
  digitalWrite(9, LOW);   //light off
  delay(blinkRate);        //wait for sop long
}

(sorry for the fact that some of the code appears to be commented even though it’s not according to the compiler)

As you can see from the above code it is quite simple, nothing an engineer with one or two coding classes shouldn’t be able to handle.  However, where my engineering colleagues in class lack in coding, they more than makeup in their hardware and circuit building knowledge.  While I may spend 20 minutes trying to figure out how a circuit goes together on a bread board, they may have it done and working in about 5 minutes.  So I feel that the fact that I am getting exposed to this will help me as a computer scientist in the future, because I will have a basic knowledge of hardware on top of my software knowledge.  And hopefully by the end of the semester I will be able to teach many of the engineers some of the coding skills I have learned, while they teach me some of their circuit building tricks, because in the end both teams have to work together to make the product a reality in industry.

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: