Taking input from Processing and transmitting it wirelessly to control a remote set of leds

The nrf library allows us to send only integers , characters , bytes but not Strings objects (you can definitely send character arrays i.e strings). But it is really easy to communicate from Processing to Arduino using String objects because of the ease and the many ways in which we can manipulate Strings in complex ways. Unfortunately we can’t do anything about that , so here we will send a String object from Processing , store it in a byte/character array in Arduino then we will send that from one node to other.

As you saw in the last video how we controlled a set of leds by providing input from processing to Arduino(which was meant to be a simplified simulation of a more realistic situation where u control a set of actuators). After that we clearly see the limitations that we have to carry the laptop wherever the bot goes(no problem if it is a robotic arm). So here is a solution , wherein i send the data from processing to a node that transmits it wirelessly to another node and which in turn controls a set of actuators/leds.

This is the video –

Processing code is exactly the same as for the previous post —

import processing.serial.*;
Serial myport;
String plus = "+";
String subt = "-";
String s1 = "", s2= "";
void setup()
{
  size(600, 600);
  smooth();
  String portname = "COM3";
  myport = new Serial(this, portname, 57600);
}
void draw()
{
  background(204);
  ellipse(300, 300, 510, 510);
  line(300, 300, mouseX, mouseY);
  if ((mouseX - 300) >= 0)
  {
    s1 = plus+(mouseX-300);
  } else
  {
    s1 = subt+(300-mouseX);
  }
 
  if ((300 - mouseY) >= 0)
  {
    s2 = plus+(300-mouseY);
  } else
  {
    s2 = subt+(mouseY-300);
  }
  myport.write(s1+s2+';'+'\n');
  println(s1+s2);
}

Code for the node to which Processing sends its data/ Transmitter —

#include<SPI.h>
#include"nRf24L01.h"
#include"RF24.h"
#include"printf.h"
RF24 radio(9, 10);
const uint64_t pipes[1] = { 0xF0F0F0F0D2LL };
bool go_to_tx_mode = false;
char input[10] = "";
void setup() {
  pinMode(7,OUTPUT);
  digitalWrite(7,LOW);
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.enableDynamicPayloads();
  radio.setRetries(15, 15);
  radio.openWritingPipe(pipes[0]);
  radio.startListening();
  radio.printDetails();
  // put your setup code here, to run once:
 
}
 
void loop() {
  if (Serial.available())
  {
    String rec = Serial.readStringUntil('\n');
    rec.toCharArray(input,10);
    if(input[0] == '+')
    {digitalWrite(7,HIGH);}
    else
    {digitalWrite(7,LOW);}
    go_to_tx_mode = true;
   
  }
  if (go_to_tx_mode)
  {
    radio.stopListening();
    bool ok = radio.write(input, 10);
    if (ok)
    {     
      go_to_tx_mode = false;
    }
  }
}

Receiver code —

//the same program will run on both the nodes.here any one can initiate the chat and any node can transmit or receive any number of messages in any order.
#include<SPI.h>
#include"nRf24L01.h"
#include"RF24.h"
#include"printf.h"
RF24 radio(9, 10);
const uint64_t pipes[1] = { 0xF0F0F0F0D2LL };
char input[10] = "";
const int red_led = 5;
const int orange_led = 7;
const int blue_led = 3;
const int green_led = 6;
void setup() {
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(6, LOW);
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.enableDynamicPayloads();
  radio.setRetries(15, 15);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
  radio.printDetails();
  // put your setup code here, to run once:
 
}
 
void loop() {
  if (radio.available())
  {
    char payload[10] = "";
    bool done = false;
    while (!done)
    {
      done = radio.read( payload , 10 );
      printf("%s\n", payload);
      glow_them(payload);
    }
  }
}
void glow_them(char msg[10])
{
  if (msg[2] == '+' || msg[2] == '-')
  {
    if (msg[2] == '+')
    {
      digitalWrite(green_led, LOW);
    }
    else if (msg[2] == '-')
    {
      digitalWrite(green_led, HIGH);
    }
    if (msg[4] == ';')
    {
      analogWrite(red_led, msg[3]);
    }
    else if (msg[5] == ';')
    {
      analogWrite(red_led, 10 * msg[3] + msg[4]);
    }
    else if (msg[6] == ';')
    {
      analogWrite(red_led, 100 * msg[3] + 10 * msg[4] + msg[5]);
    }
    if (msg[0] == '+')
    {
      digitalWrite(orange_led, LOW);
      analogWrite(blue_led, msg[1]);
    }
    else if (msg[0] == '-')
    {
      digitalWrite(orange_led, HIGH);
      analogWrite(blue_led, msg[1]);
    }
  }
  else if (msg[3] == '+' || msg[3] == '-')
  {
    if (msg[5] == ';')
    {
      analogWrite(red_led, msg[4]);
    }
    else if (msg[6] == ';')
    {
      analogWrite(red_led, 10 * msg[4] + msg[5]);
    }
    else if (msg[7] == ';')
    {
      analogWrite(red_led, 100 * msg[4] + 10 * msg[5] + msg[6]);
    }
    if (msg[3] == '+')
    {
      digitalWrite(green_led, LOW);
    }
    else if (msg[3] == '-')
    {
      digitalWrite(green_led, HIGH);
    }
    if (msg[0] == '+')
    {
      digitalWrite(orange_led, LOW);
      analogWrite(blue_led, 10 * msg[1] + msg[2]);
    }
    else if (msg[0] == '-')
    {
      digitalWrite(orange_led, HIGH);
      analogWrite(blue_led, 10 * msg[1] + msg[2]);
    }
  }
  else if (msg[4] == '+' || msg[4] == '-')
  {
    if (msg[6] == ';')
    {
      analogWrite(red_led, msg[5]);
    }
    else if (msg[7] == ';')
    {
      analogWrite(red_led, 10 * msg[5] + msg[6]);
    }
    else if (msg[8] == ';')
    {
      analogWrite(red_led, 100 * msg[5] + 10 * msg[6] + msg[7]);
    }
    if (msg[4] == '+')
    {
      digitalWrite(green_led, LOW);
    }
    else if (msg[4] == '-')
    {
      digitalWrite(green_led, HIGH);
    }
    if (msg[0] == '+')
    {
      digitalWrite(orange_led, LOW);
      analogWrite(blue_led, 100 * msg[1] + 10 * msg[2] + msg[3]);
    }
    else if (msg[0] == '-')
    {
      digitalWrite(orange_led, HIGH);
      analogWrite(blue_led, 100 * msg[1] + 10 * msg[2] + msg[3]);
    }
  }
  // put your main code here, to run repeatedly:
 
}

 

Advertisements

About Shantam Raj

Currently I am a final year B.Tech undergraduate majoring in “Electronics and Communication Engineering” from IIT Guwahati. I am passionate about electronics and robotics. Apart from that I love writing, visit https://medium.com/@shantam_raj for some of my works. I am a die hard soccer fan. I also love to play badminton. I am a hardware hacking enthusiast and a tinkerer !!.
This entry was posted in Arduino, nRF24L01+, Processing, Serial Communication, Strings, Wireless and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s