tisdag 29 september 2015

Prototyping

We have continued working with the bluetooth connection. Currently a mobile phone acts as a serial server that can send and receive information. Our Arduino setup consists of the bluetooth, one RGB-LED light and a pressure sensor. The light can only be changed at the exact moment a command is sent from the mobile. When a command is sent the program compares the command to the pressure registered from the sensor and sets the light to the highest ranking color.

Blue: Lowest pressure
Red: Medium pressure
Green: Highest pressure



Setup before the pressure sensor was added


Code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 16); // RX, TX

int r = 4;
int g = 3;
int b = 2;

void setup() {

  Serial.begin(9600);
  
  pinMode(r,OUTPUT);
  pinMode(g,OUTPUT);
  pinMode(b,OUTPUT);
  
  mySerial.begin(9600);
}

void loop()
{  
  while (mySerial.available()){
    int sensorValue = analogRead(A0);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (3 / 1023.0);
    
    char test_sensor;
    if(voltage>=2){
      test_sensor = 'r';
      }
    else if(voltage>=1){
      test_sensor = 'b';
      }
    else if(voltage>=0){
      test_sensor = 'g';
      }
    char test = mySerial.read();
    digitalWrite(r,LOW);
    digitalWrite(g,LOW);
    digitalWrite(b,LOW);
    if(test=='r' or test_sensor == 'r')
    {
      digitalWrite(r,HIGH);
      Serial.println(voltage);
    }
    else if(test=='b' or test_sensor == 'b')
    {
      digitalWrite(b,HIGH);
      Serial.println(voltage);
    }
    else
    {
      digitalWrite(g,HIGH);
      Serial.println(voltage);
    }
  }
}

By: Carolina, Josefine, Lennart, Julius

Inga kommentarer:

Skicka en kommentar