Go Down

Topic: Arduino UNO - 433MHz (Read 84 times) previous topic - next topic

Jordo

Hello everyone,

a few weeks ago i started with my home automation project.
The project is working with an Arduino Mega (Server) and 2 arduino Uno (Clients).

The server gets commandos from the user and send them to the right client.
For sending i use the VirtualWire library. I am also using Time.h, but i dont think this would be the problem.
Nothing wrong with the sender, I have test this one a few times.

One of the codes is working very great, and receives all messages, and work out its commands.
For example 3 AnalogWrite commands for an RGB LED strip.
The other one is working good until AnalogWrite is used. The Arduino keeps running, but the 433 receiving stops.

For both Clients i use PWM pins 3, 5, 6 and 11. Because the timer of pin 9 and 10 is used by VirtualWire.

I asked a lot of people and read a lot of sites looking for a solution.
Nothing helped, so i hope one of you could take a look at my code and come with a solution.

Code: [Select]
#include <Time.h>
#include <TimeLib.h>

#include <VirtualWire.h>

int txMHz = 13;
int rxMHz = 11;
const byte LEDPin[] = {11, 3};
byte LED_OLD[] = {0, 0};
byte maxLight = 200;

const byte OutputArray[] = {5, 7, 8, 2, 4, 12, 6, 0, 0, 0, 10, 11}; //Used for pins examp: item 0 = pin 5

typedef struct {
  byte destination[3];    //Client Mac
  byte actie;             //Is used to set action for client
  byte state;             //ON/OFF
  byte num;
  byte kleur[7];          //Is used for sending hex color (6 bytes) and time (7 bytes)
  byte percentage;        //Light percentage
  byte CRC;
} message;
message *input;
message output;

byte myMacAdress[] = {'C', 'C', '1'};
uint8_t buflen = VW_MAX_MESSAGE_LEN;
uint8_t buf[VW_MAX_MESSAGE_LEN];
byte stringOutput[sizeof(output)];

void setup() {
  vw_setup(1200);
  vw_rx_start();       // Start the receiver PLL running
  vw_set_tx_pin(10);
  vw_set_rx_pin(9);
  vw_set_ptt_inverted(true); // Required for DR3100

  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  analogWrite(LEDPin[1], 0); //pin 3
  analogWrite(LEDPin[0], 0); //pin 11

  Serial.begin(9600);
}

void loop() {
  vw_wait_rx_max(500);
  if (vw_get_message(buf, &buflen))
  {
    Serial.println("Message");
    digitalWrite(13, HIGH);
    separate(); // splits op
    if (input->actie == 5)
    {
      Actie(input->actie);
    }
    else
    {
      if (copyDetection())
      {
        if (checkDest())
        {
          Serial.println("For me");
          Actie(input->actie);
        }
      }
    }
    emptyInput();
    digitalWrite(13, LOW);
  }
}

void setColor(int percentage, int LED) //Set white LEDs
{
  int value;
  float F;

  F = 2.55 * percentage;
  value = (int) F;

  LED = LED - 1;

  if (value > maxLight)
  {
    value = maxLight;
  }

  if (value < LED_OLD[LED])
  {
    for (int i = LED_OLD[LED]; i > value; i -= 3) //Fade out light
    {
      analogWrite(LEDPin[LED], i);
      delay(50);
    }
    analogWrite(LEDPin[LED], value);
    LED_OLD[LED] = value;
  }
  else
  {
    for (int i = LED_OLD[LED]; i < value; i += 3) //Fade out light
    {
      analogWrite(LEDPin[LED], i);
      delay(50);
    }
    analogWrite(LEDPin[LED], value);
    LED_OLD[LED] = value;
  }
}


void Outputs(int pin, int OnOff) //Set outputs
{
  Serial.print("Pin: ");
  Serial.println(pin);
  if (pin == 10)
  {
    //Alles links
    digitalWrite(OutputArray[0], OnOff);
    delay(100);
    digitalWrite(OutputArray[1], OnOff);
    delay(100);
    digitalWrite(OutputArray[2], OnOff);
    delay(100);
    if (OnOff == 1)
    {
      setColor(40, 1);
    }
    else
    {
      setColor(0, 1);
    }
  }
  else if (pin == 11)
  {
    //Alles rechts
    digitalWrite(OutputArray[3], OnOff);
    delay(100);
    digitalWrite(OutputArray[4], OnOff);
    delay(100);
    digitalWrite(OutputArray[5], OnOff);
    delay(100);
    if (OnOff == 1)
    {
      setColor(40, 2);
    }
    else
    {
      setColor(0, 2);
    }
  }
  else
  {
    digitalWrite(pin, OnOff);
  }
  Serial.println("Outputs set");
  //LED links= 2 aangesloten op pin 10, rechts= 6 aangesloten op pin 8
}

void separate() { //Place incomming bytes in struct
  input = (message*)buf;
}

boolean checkDest() { //Check if message is for me
  byte c = 0;
  for (byte i = 0; i < 3; i++) {
    if (input->destination[i] == myMacAdress[i]) {
      c = 1;
    }
    else {
      c = 0;
      break;
    }
  }
  return c;
}

void emptyInput() {              //Empty the input struct
  for (byte i = 0; i < 3; i++) {
    input->destination[i] = 0;
  }
  for (int i = 0; i < 6; i++)
  {
    input->kleur[i] = 0;
  }
  input->actie = 0;
  input->state = 0;
  input->num = 0;
  input->percentage = 0;
  input->dag = 0;
}

void Actie(int i)
{
  int x = 0;
  switch (i)
  {
    case 1:
      setColor(input->percentage, input->num);
      break;

    case 2:
      break;

    case 3:
      x = OutputArray[input->num];
      Outputs(x, input->state);
      break;

    case 4:
      break;

    case 5:
      setDateTime();
      break;

    case 6:
      break;

    default:
      break;
  }
}

void setDateTime()
{
  int jaar = input->kleur[5] * 100;
  jaar = jaar + input->kleur[6];
  setTime(input->kleur[0], input->kleur[1], input->kleur[2], input->kleur[3], input->kleur[4], jaar);
}

boolean copyDetection() //CRC check
{
  byte c = 0;
  if (input->dag == day())
  {
    c = 1;
  }
  else
  {
    c = 0;
  }

  return c;
}


Thanks for reading. If you have questions, i would like to here them.
I look forward to a reply

- Jordo

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy