Saturday, April 18, 2015

Converting a number from digital to binary and show the binary form using leds

In this improvement, we are getting integer numbers from serial port. With modula operation (%2) and with some basic calculus, we convert this integer number to binary form. Our last step is showing the binary representation of this number using 8 leds. The photo of this improvement is given below.

İlker Çam & Berrak Şişman



int datapin = 2;
int clockpin = 3;
int latchpin = 4;

byte data = 0;


void setup()
{
  // Set the three SPI pins to be outputs:

  pinMode(datapin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(latchpin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Ready to read...");
}


void loop()
{


  if(Serial.available() > 0){
    int readFromSerial = Serial.parseInt();
    Serial.println("Readed From Input");
    Serial.println(readFromSerial);
    readIntAsBinary(readFromSerial);
  }
}

void readIntAsBinary(int number){
  int tmp = number;
  int counter;


  for(int i = 0;i<=7;i++){
    shiftWrite(i, LOW);
    delay(10);
  }
  do{
    int b = tmp%2;
    Serial.println(b);
    tmp/=2;
    shiftWrite(counter++, b==0 ? LOW : HIGH);
    delay(20);
  }while(tmp>0);
  for(;counter<=7;counter++){
    shiftWrite(counter,LOW);
    delay(20);
  }
}


void shiftWrite(int desiredPin, boolean desiredState)
{
  bitWrite(data,desiredPin,desiredState);

  shiftOut(datapin, clockpin, MSBFIRST, data);

  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin, LOW);
}

void oneAfterAnother()
{
  int index;
  int delayTime = 100; // Time (milliseconds) to pause between LEDs
                       // Make this smaller for faster switching

  for(index = 0; index <= 7; index++)
  {
    shiftWrite(index, HIGH);
    delay(delayTime);              
  }

  for(index = 7; index >= 0; index--)
  {
    shiftWrite(index, LOW);
    delay(delayTime);
  }
}


/*
oneOnAtATime()

This function will step through the LEDs, lighting one at at time.
*/

void oneOnAtATime()
{
  int index;
  int delayTime = 100; // Time (milliseconds) to pause between LEDs
                       // Make this smaller for faster switching

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)
  {
    shiftWrite(index, HIGH);    // turn LED on
    delay(delayTime);       // pause to slow down the sequence
    shiftWrite(index, LOW); // turn LED off
  }
}


/*
pingPong()

This function will step through the LEDs, lighting one at at time,
in both directions.
*/

void pingPong()
{
  int index;
  int delayTime = 100; // time (milliseconds) to pause between LEDs
                       // make this smaller for faster switching

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)
  {
    shiftWrite(index, HIGH);    // turn LED on
    delay(delayTime);       // pause to slow down the sequence
    shiftWrite(index, LOW); // turn LED off
  }

  // step through the LEDs, from 7 to 0

  for(index = 7; index >= 0; index--)
  {
    shiftWrite(index, HIGH);    // turn LED on
    delay(delayTime);       // pause to slow down the sequence
    shiftWrite(index, LOW); // turn LED off
  }
}


/*
randomLED()

This function will turn on random LEDs. Can you modify it so it
also lights them for random times?
*/

void randomLED()
{
  int index;
  int delayTime = 100; // time (milliseconds) to pause between LEDs
                       // make this smaller for faster switching

  // The random() function will return a semi-random number each
  // time it is called. See http://arduino.cc/en/Reference/Random
  // for tips on how to make random() more random.

  index = random(8);    // pick a random number between 0 and 7

  shiftWrite(index, HIGH);  // turn LED on
  delay(delayTime);     // pause to slow down the sequence
  shiftWrite(index, LOW);   // turn LED off
}


/*
marquee()

This function will mimic "chase lights" like those around signs.
*/

void marquee()
{
  int index;
  int delayTime = 200; // Time (milliseconds) to pause between LEDs
                       // Make this smaller for faster switching

  // Step through the first four LEDs
  // (We'll light up one in the lower 4 and one in the upper 4)

  for(index = 0; index <= 3; index++)
  {
    shiftWrite(index, HIGH);    // Turn a LED on
    shiftWrite(index+4, HIGH);  // Skip four, and turn that LED on
    delay(delayTime);       // Pause to slow down the sequence
    shiftWrite(index, LOW); // Turn both LEDs off
    shiftWrite(index+4, LOW);
  }
}


/*
binaryCount()

Numbers are stored internally in the Arduino as arrays of "bits",
each of which is a 1 or 0. Just like the base-10 numbers we use
every day, The position of the bit affects the magnitude of its
contribution to the total number:

Bit position   Contribution
0              1
1              2
2              4
3              8
4              16
5              32
6              64
7              128

To build any number from 0 to 255 from the above 8 bits, just
select the contributions you need to make. The bits will then be
1 if you use that contribution, and 0 if you don't.

This function will increment the "data" variable from 0 to 255
and repeat. When we send this value to the shift register and LEDs,
you can see the on-off pattern of the eight bits that make up the
byte. See http://www.arduino.cc/playground/Code/BitMath for more
information on binary numbers.
*/

void binaryCount()
{
  int delayTime = 1000; // time (milliseconds) to pause between LEDs
                        // make this smaller for faster switching

  // Send the data byte to the shift register:

  shiftOut(datapin, clockpin, MSBFIRST, data);

  // Toggle the latch pin to make the data appear at the outputs:

  digitalWrite(latchpin, HIGH);
  digitalWrite(latchpin, LOW);

  // Add one to data, and repeat!
  // (Because a byte type can only store numbers from 0 to 255,
  // if we add more than that, it will "roll around" back to 0
  // and start over).

  data++;

  // Delay so you can see what's going on:

  delay(delayTime);
}

LCD improvement

It is the first time that we used LCD in this course. In this improvement, we used timer, interrupt and show the current temperature on LCD screen. We used setCursor() function for the location of the data on the screen. We used analogRead function while reading the temperature value.
                                             Berrak Şişman and İlker Çam


#include 
#include 


const int temperaturePin = 0;
LiquidCrystal lcd(12,11,5,4,3,2);
volatile int readEvent = 0;
void setup()
{

  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Read Serial!");
  Serial.begin(9600);
  Timer1.initialize(8000000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt(setFlag); // attach the service routine here
}

void loop()
{
    if(readEvent == 2){
      lcd.setCursor(0,1);
      lcd.print("Interrupt Calling");
      Serial.println("Interrupt Calling..");
      getTemp();
      readEvent = 0;
    }
}

void setFlag(){
  readEvent++;
}

void getTemp()
{
  float voltage,degreesC;
  voltage = (analogRead(temperaturePin) * 0.004882814);
  degreesC = (voltage - 0.5) * 100.0;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temperature : ");
  lcd.print(degreesC);
}

Make a decision and change the speed of the motor : 2 push buttons and data taken from serial port (0 or 1)

In this improvement, we take binary data 0 or 1 from serial port. We have 2 push buttons. With using these three things, we make a decision; whether the speed of the motor increases or decreases. If our first button state is low, and the second one is high and also the data taken from serial port is 1 then, we choose to increase the speed. The other case is   the data taken from serial port is 1 but the button states are inverse of the first case, meaning that first button state low and second button state is high, we decrease the speed. In this code we used analogWrite() function while changing the value of speed. The photo of this improvement is given below.

Berrak Şişman & İlker Çam


Arduino code:


const int btn1Pin = 2;
const int btn2Pin = 3;
const int motorPin = 9;

int theBitThatIReadFromSerialIfTheMotorIsRunningOrNot = 1;
int motorSpeed = 0;
void setup()
{
  pinMode(motorPin, OUTPUT);
  pinMode(btn1Pin, INPUT);
  pinMode(btn2Pin, INPUT);
  Serial.begin(9600);
}


void loop()
{
     //serialSpeed();
     motor();
}

void motor(){
  int btnState, btn2State;
  btnState = digitalRead(btn1Pin);
  btn2State = digitalRead(btn2Pin);

  if(btnState == LOW && btn2State == HIGH && theBitThatIReadFromSerialIfTheMotorIsRunningOrNot == 1){
    motorSpeed = (motorSpeed+40);
    motorSpeed = constrain(motorSpeed,0,255);
    analogWrite(motorPin, motorSpeed);
  }else if(btnState == HIGH && btn2State == LOW && theBitThatIReadFromSerialIfTheMotorIsRunningOrNot == 1){
    motorSpeed = (motorSpeed-40);
    motorSpeed = constrain(motorSpeed,0,255);
    analogWrite(motorPin, motorSpeed);
  }

  if(Serial.available() > 0){
    int readed = Serial.parseInt();
    if(readed == 1){
      theBitThatIReadFromSerialIfTheMotorIsRunningOrNot = 1;
      if(motorSpeed<=0){
        motorSpeed = 200;
        analogWrite(motorPin, motorSpeed);
      }
    }else if(readed == 0){
      theBitThatIReadFromSerialIfTheMotorIsRunningOrNot = 0;
      if(motorSpeed>0){
        motorSpeed = 0;
        analogWrite(motorPin, motorSpeed);
      }
    }
  }

  delay(100);
}