ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The question really was for me to use the message to carry out more than one action. The solution that I found was to The solution I came up with for the specific problem was to take another output to act as a switch and have its value control the actions to be executed in the loop section where the actual blinking is implemented. Please see the code below:

#include <ros.h>

#include <std_msgs/Empty.h>

ros::NodeHandle  nh;

void messageCb( const std_msgs::Empty& toggle_msg){
  digitalWrite(8, HIGH-digitalRead(8));   // blink the led
}

ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );

void setup()
{ 
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
  nh.initNode();
  nh.subscribe(sub);
}

void loop()
{  
  nh.spinOnce();
   if(digitalRead(8) >= HIGH){
    digitalWrite(13, HIGH-digitalRead(13));
    delay(100);
    digitalWrite(13, HIGH-digitalRead(13));
    delay(100);
 } // blink the led
}

What I also gathered was that you actually can put as many commands as you want into your message like so:

void messageCb( const std_msgs::Empty& toggle_msg){
  for (int thisInt = 1; thisInt < 2000; thisInt++) {
  digitalWrite(8, HIGH-digitalRead(8)); delay(100); 
// blink the led
}}

This obviously makes it blink 1000 times.