fredag 2 oktober 2015

Hack lab: Server

This hack lab we tried to get the server to work for both the Arduinos at the same time. We started off by changing the program so that the lights changes color depending on the pressure that are sent from the server, this did not work the last time.

At first we could only make use of the data from one of the Arduinos on the server. By sending the data we could use the pressure from one sensor to turn on the correct light on both Arduinos. However, when we tried to use the data from both Arduinos at the same time the program ran incredibly slow. This took some time to solve but we finally managed.

So this is how the system works:

  • The Arduinos takes the current pressure, converts it to the matching color, checks if the color has changed from before and if it has sends it to the server.
  • The server compares the data from both Arduinos and sends out the highest ranking color to both Arduinos.
  • The Arduino turns off all lights and turns on the right one if data is collected from the server.



Code for Arduino:

#include <SoftwareSerial.h>

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

int r = 4;
int g = 3;
int b = 2;
int test;
char check;
char light;

void setup() {

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

void loop()
{
   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();
    
Serial.println(voltage);
    
    if(check != test_sensor){
      
    if(test_sensor == 'r')
    {
      mySerial.print(test_sensor);
      Serial.println(voltage);
      check = test_sensor;
    }
    else if(test_sensor == 'b')
    {
      mySerial.print(test_sensor);
      Serial.println(voltage);
      check = test_sensor;
    }
    else if(test_sensor == 'g')
    {
      mySerial.print(test_sensor);
      Serial.println(voltage);
      check = test_sensor;
    }
    }
     while(mySerial.available()){
        light = mySerial.read();
        digitalWrite(r,LOW);
        digitalWrite(g,LOW);
        digitalWrite(b,LOW);
        Serial.println(light);
        if(light == 'r'){
          digitalWrite(r,HIGH);
          }
          else if(light == 'b'){
          digitalWrite(b,HIGH);
          }
          else if(light == 'g'){
          digitalWrite(g,HIGH);
          }
      }
}

Code for server:

# Imports
import time
import serial
from Tkinter import *

# Serial port parameters
serial_speed1 = 9600
serial_speed2 = 9600
serial_port1 = '/dev/tty.banana-DevB'
serial_port2 = '/dev/tty.HC-06-DevB'

# Test with USB-Serial connection
# serial_port = '/dev/tty.usbmodem1421'

ser1 = serial.Serial(serial_port1, serial_speed1, timeout=0.1)
ser2 = serial.Serial(serial_port2, serial_speed2, timeout=0.1)

colors=['r','b','g']
for i in range (3):
for j in range (3):
ser1.write(colors[j])
time.sleep(0.05)
ser2.write(colors[j])
time.sleep(0.05)

print("Ready!")

data1 = ''
data2 = ''

while 1:
#ser1.flushInput()
#ser2.flushInput()

temp1 = ser1.read()
temp2 = ser2.read()

if(temp1 != ''):
data1 = temp1

if(temp2 != ''):
data2 = temp2

#print("from1:" + data1)
#print("from2:" + data2)

if(data1 == 'r' or data2 == 'r'):
ser1.write('r')
ser2.write('r')
elif(data1 == 'b' or data2 =='b'):
ser1.write('b')
ser2.write('b')
else:
ser1.write('g')
ser2.write('g')


We have also been discussing how the cube should be constructed, we're thinking about using a 3D printer. But depending on which sensor we're going to use the cube might have to be air tight (if we use e.g. air pressure). This is the next challenge in cog process.

By: Caroline, Josefine, Lennart, Julius

Inga kommentarer:

Skicka en kommentar