Friday 20 April 2012

Version 3 Development: work on relay function

I wanted the button to work by turning something on for x amount of seconds and then going off. To do this i added an extra external variable 'indicatorVar' as well as an extra LED on pin 1.


  if(indicatorVar==1) {
    timerVar = 1;
 
  }

  if(timerVar==1) {
  digitalWrite(ledpin2, HIGH);
  delay(5000);
    digitalWrite(ledpin2, LOW);
    timerVar = 0;
  }

With this example the delay does interrupt the program flow, but this means that duplicate messages cannot be sent which is a benefit. All i need to do now is make a few edits to the code on this side as well as that on the other side.

In the main code of the communicator xbee changed the trigger back to the original 'pushbutton'. In the code on the relay xbee i added the delay code to the main code.

I moved the code so that the indicator lights are always off, and stay solidly on rather than 'blinking' this will help to reduce the delays within the code to speed it up.

I noticed that the transmitting xBee continued to send messages long after it has been pressed, so i decided to incorporate debounce code. I also moved my relay code from inside the 'TimerVar' conditional. These two actions helped with reducing the repeating problem, but now it still repeating twice. I am going to try and use a switch operative which may help me exit the loop.

Part the conditional i removed:


 timerVar = 1;
   
      if(timerVar==1) {
   
      digitalWrite(relay, HIGH);
  delay(5000);
    digitalWrite(relay, LOW);
    delay(1000);
      }

I then changed the 'if' statement to a switch one. I used the arudino reference library to remind me of the exact syntax.


switch (var) {
    case 1:
      //do something when var equals 1
      break;
    case 2:
      //do something when var equals 2
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
  }


I had to change the transmitting to do equal numbers rather than letters.

However, this switch case still isn't improving the repetition problem.


digitalWrite(relay, HIGH);
  delay(5000);
    digitalWrite(relay, LOW);
    delay(1000); //var equals 1

this basic code works, but repeats even when within the switch framework, as below


switch (val) {
    case 'D':
 digitalWrite(relay, HIGH);
  delay(5000);
    digitalWrite(relay, LOW);
    delay(1000); //var equals 1
      break;
    case 'H':
      //do something when var equals 2
      break;
 
   
  }

The next thing i'm going to try to get around this problem is increasing the delay of  AND the delay on the debounce code.

Debounce code delay has been increased to 100

long debounceDelay = 100;    // the debounce time; increase if the output flickers


I increased the serial.write delay all the way upto 10,000 but it is still repeating twice.

The serial monitor shows that the arduino is only sending a single 'D'. I am going to post this in the arduino forums and see if i can find an answer. It might be good for the system to repeat the message twice, but i'd like it to be a design choice.

The breadboard layouts are very similar to before, see below.




I just wrote in the feedback code (where USER A knows that their message has been sent to USER B, and this exacerbated the the message duplication issue-it repeated 3 times rather than the 2).

The code is good enough to take to the next focus group, i just need to decide which contacts to add onto the wires in the bears hands (to close the circuit), get hold of larger teddies, wait for the longer wires to arrive and   incorporate the system into a new teddy bear. In the meantime i want to develop the branding and presentation of the teddy bears i.e. packaging, website, advert/video and of course develop an instruction manual. This will all be developed in the sketchbook and will be presented to users at the same time as the version 3 prototype.

No comments:

Post a Comment