Because of these changes we had to modify the code somewhat. The cubes do not need to decide on the color of the lights anymore but only receive information from the server. The platform sends the current color to the server which passes the information on to the cubes.
Code for the Arduinos:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 16); // RX, TX
int r = 2;
int g = 3;
int b = 4;
char light;
void setup() {
Serial.begin(9600);
pinMode(r,OUTPUT);
pinMode(g,OUTPUT);
pinMode(b,OUTPUT);
mySerial.begin(9600);
}
void loop()
{
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);
}
}
}
#include <SoftwareSerial.h>
//SoftwareSerial mySerial(8, 9); // RX, TX
int test;
char check;
void setup() {
Serial.begin(9600);
//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();
if(check != test_sensor){
if(test_sensor == 'r')
{
Serial.print(test_sensor);
check = test_sensor;
}
else if(test_sensor == 'b')
{
Serial.print(test_sensor);
check = test_sensor;
}
else if(test_sensor == 'g')
{
Serial.print(test_sensor);
check = test_sensor;
}
}
}
Code for the server:
# Imports
import time
import serial
from Tkinter import *
# Serial port parameters
serial_speed1 = 9600
serial_speed2 = 9600
serial_speed3 = 9600
serial_port1 = '/dev/tty.banana-DevB'
serial_port2 = '/dev/tty.HC-06-DevB'
serial_port3 = '/dev/tty.usbmodem1411'
ser1 = serial.Serial(serial_port1, serial_speed1, timeout=1)
ser2 = serial.Serial(serial_port2, serial_speed2, timeout=1)
ser3 = serial.Serial(serial_port3, serial_speed3, timeout=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!")
while 1:
data = ser3.read()
print(data)
ser1.write(data)
ser2.write(data)
After the crit Carolina and Lennart went to Kjell & Company to buy the rest of the material we needed to build two cubes. We bought one more Arduino Pro Micro, one RGB LED light, two 9V batteries and two batterie contacts.
By: Carolina, Lennart, Josefine, Julius
Inga kommentarer:
Skicka en kommentar