Physical elements
I had to solder new wires onto a new mp3 player to be controlled by the arduino as the old one was fried. Hopefully this wont happen again, and i am being much more careful with the wiring this time. I discovered that a different connection has less interference (through trial and error), so i wired it differently this time.
The next step is to make the relay work on a trigger. I am going to try using a potentiometer circuit from http://www.oomlout.com/oom.php/products/ardx/circ-08 as a trigger. i've wired it up and run the test code.
This is the first piece of code i tried. The aim was to get the relay delay to respond to the potentiometer value
if(sensorValue<10) {
digitalWrite(relay, LOW); // set the relay ON
delay(sensorValue*10);
digitalWrite(relay, HIGH); // set the relay OFF
delay(sensorValue*10);
};
It didnt work as anticipated, and stopped when the value was too high. I realised that this was because i had the '<' sign the wrong way round. It worked when this was fixed!
I then tried this code
if(sensorValue<100) {
digitalWrite(relay, LOW); // set the relay ON
delay(1000);
digitalWrite(relay, HIGH); // set the relay OFF
delay(1000);
};
When the potentiometer is turned beyond a certain point the relay flashes. I took a quick video of it working below.
I now understand that the problem with the trigger yesterday was the lack of conitunuity. I can just say 'if a letter 'd' is recieved... do this', because it only happens momentarily. It needs to affect an external variable. Probably with a delay attached to reset itself.
I am also going to incorporate the potentiometer into the new prototype to see how users respond to it and ask if they'd prefer to press a button instead.
I have incorporated it into the trigger code
if (val == 'D'){
//ring the bell briefly
Serial.print('K');
digitalWrite(REMOTEINDICATOR, HIGH);
delay(10);
digitalWrite(REMOTEINDICATOR, LOW);
delay(10);
globalset = 0;
}
}
Serial.println(globalset);
}
However, the globalset code isnt executing so i am going to try something else
if (val == 'D'){
//ring the bell briefly
Serial.print('K');
digitalWrite(REMOTEINDICATOR, HIGH);
delay(10);
digitalWrite(REMOTEINDICATOR, LOW);
delay(10);
digitalWrite(relay, LOW); // set the relay ON
delay(1000);
digitalWrite(relay, HIGH); // set the relay ON
delay(1000);
int globalset = 0;
}
This successfully 'blinks' the LED controlled by the relay every second.
However, it goes on for slightly too long.
I think that this could be because of the extra delays written into the code.
PROBLEM: response time of the xBees has slowed-they are still responding a while after the button has been pressed. I will post this on the arduino forums to see if it is for any other reason other than the introduction of additional code.
I've managed to get the relay to switch on when the button is pressed and stay on. This is the source code
Added this at the bottom
globalset = 0;
delay(10);
Serial.println(globalset);
and this code to handle the inpute
if(globalset == 1) {
digitalWrite(relay, HIGH); // set the relay OFF
delay(10);
}
if(globalset == 0) {
digitalWrite(relay, LOW); // set the relay ON
delay(10);
}
I now need to either change the code of the other device to send a signal when the button is not being pressed (computationally expensive), OR to write an extra bit of code (i'll have to keep tweaking things if i add other functions).
I will add a condition based on Serial.read() which i think says that if anything but a 'd' is received, to turn the relay off
if (Serial.read() != 'D') {
globalset = 1;
}
However this doesnt work.
I think i will try adding code to both.
CIRCUIT 1 (with relay):
if (val == 'h'){
globalset = 0;
delay(10);
Serial.println(globalset);
}
DEVICE 2 (with pushbutton):
if (digitalRead(BUTTON) == HIGH) {
Serial.print('h');
delay(10); // prevents overwhelming the serial port i increased the delay
}
SUCCESS: This now works perfectly!!!
The next step is to incorporate the MP3 player into the system by plugging out the LED and replacing it.
This functions correctly! However the pushbutton is very fiddly and kept popping out of the circuit so i will replace it with the potentiometer.
SOURCE CODE
CIRCUIT 1
//this is the device with pushbutton
#define VERSION "1.00a0"
int BELL= 11;
int REMOTEINDICATOR= 7;
int BUTTON = 2;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(BELL, OUTPUT);
pinMode(REMOTEINDICATOR, OUTPUT);
Serial.begin(9600);
}
void loop() {
// digitalWrite(BELL, HIGH);
char s2 = Serial.read();
// send a capital D over the serial port if the button is pressed
if (digitalRead(BUTTON) == LOW) {
Serial.print('D');
delay(10); // prevents overwhelming the serial port i increased the delay
}
if (digitalRead(BUTTON) == HIGH) {
Serial.print('h');
delay(10); // prevents overwhelming the serial port i increased the delay
}
// digitalWrite(BELL, HIGH);
if (Serial.available() > 0) {
// Serial.print(s2);
if (Serial.read() == 'K'){
//ring the bell briefly
// Serial.print('k');
digitalWrite(REMOTEINDICATOR, HIGH);
delay(10);
digitalWrite(REMOTEINDICATOR, LOW);
delay(10);
}
if (Serial.read() == 'H'){
//ring the bell briefly
digitalWrite(BELL, HIGH);
delay(10);
digitalWrite(BELL, LOW);
delay(10);
// Serial.print('k'); response code - develop later
}
}
}
CIRCUIT 2
//this is the device with the relay on it
int globalset = 1;
int buttonState = 0; // variable for reading the pushbutton status
#define VERSION "1.01"
int REMOTEINDICATOR = 8;
int BELL = 5;
char val;
int lightsetting = 400;
//int BUTTON = 6;
int relay = 2;
//int sensorValue = 0; // variable to store the value coming from the ldr
//int photocellPin = 0;
//int photocellReading;
//int ldrPin = A0; // the input pin for the ldr
void setup() {
pinMode(BELL, OUTPUT);
// pinMode(BUTTON, INPUT);
pinMode(REMOTEINDICATOR, OUTPUT);
Serial.begin(9600);
pinMode(relay, OUTPUT);
}
void loop() {
if(globalset == 1) {
digitalWrite(relay, HIGH); // set the relay OFF
delay(10);
}
if(globalset == 0) {
digitalWrite(relay, LOW); // set the relay ON
delay(10);
}
if (Serial.available() > 0) {
val=Serial.read();
if (val == 'h'){
globalset = 0;
delay(10);
Serial.println(globalset);
}
if (val == 'D'){
//ring the bell briefly
Serial.print(val);
Serial.print('K');
digitalWrite(REMOTEINDICATOR, HIGH);
delay(10);
digitalWrite(REMOTEINDICATOR, LOW);
delay(10);
// Serial.print("hi");
// uncomment the next line if you don't have an oscillating buzzer
//tone(BELL, 440, 100);
globalset = 0;
delay(10);
Serial.println(globalset);
}
}
// Serial.println(globalset); INTERFERES WITH PROGRAM FLOW
}
Now i will be able to conduct the focus group. I have recorded a new voice clip saying 'i miss you' and it will the system has been installed into the teddy bear and will be brought to users.
I used this code to test the potentiometer and then incorporated it into the rest of the code. I commented out the button code in the relay circuit.
int globalset = 0;
//int relay = 2;
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
// pinMode(relay, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(20);
}
I had to make a final change to the code as i was setting 'globalset' to '0' in both conditionals.
This is how the final circuit with the mp3 player looks
PROBLEM: I currently have to wait 7 seconds for the potentiometer input to register. I am going to post this on the arduino forums to find out why. The response indicator light is also very sluggish with the same response time. It's a little confusing as analysis of the serial monitor shows that the message is being sent immediately, but it is not being interpreted and received immediately. I will refer to 'Building Wireless Networks for further guidance on this.
I have developed a fritzing models to support the development.
No comments:
Post a Comment