How to control ARDUINO with joystick?
Hello everyone. I am trying to control led using joystick. My joystick connect with joy node to jetson nano, jetson nano connect to Arduino also. I am sending input from my joystick to Arduino, Arduino control led. As long as I press the joystick button I want the LED to light, when I release the button I want LED to low. my led blink when I press the button but I release the button led doesn’t off. Is anyone help me to solve this problem. I read button and axes veriable from joystick but I cant transfer this data to Arduino.
This is my arduino code
#include <stdint.h>
#include <stdlib.h>
#include <ros.h>
#include <std_msgs/Float32.h>
#include <sensor_msgs/Joy.h>
void joydata ( const sensor_msgs::Joy& joy)
{
if ((joy.buttons[1] >= 0.5) )
{
digitalWrite(2, HIGH - digitalRead(2));
}
else if (joy.buttons[1] <= 0.5)
{
digitalWrite(2, LOW - digitalRead(2));
}
else {
delay(500);
digitalWrite(2, LOW - digitalRead(2));
}
}
ros::NodeHandle nh;
ros::Subscriber<sensor_msgs::Joy> sub1("joy", joydata);
void setup()
{
pinMode(2, OUTPUT); //set up the LED
nh.initNode();
nh.subscribe(sub1);
Serial.begin(57600);
}
void loop() {
nh.spinOnce();
delay(10);
}
I take from axes and button variable
header:
seq: 9606
stamp:
secs: 1571665519
nsecs: 387521698
frame_id: ''
axes: [-0.0, -0.0, -0.0, 1.0, 1.0, -0.0, -0.0, -0.0]
buttons: [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
header:
seq: 9607
stamp:
secs: 1571665523
nsecs: 24873820
frame_id: ''
axes: [-0.0, -0.0, -0.0, 1.0, 1.0, -0.0, -0.0, -0.0]
buttons: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Edit: This is final verison of my code. Sometimes Arduino doesn't read joystick input when I send data. Doesn't catch the input.
I did 0.05 ms for nh.spinOnce();
This way i can prevent this error.
I want control Arduino'S Led with joystick concurrently. Sometimes Arduino's Leds stay blink. How can I do control LED simultaneously?
#include <stdint.h>
#include <stdlib.h>
#include <ros.h>
#include <std_msgs/Float32.h>
#include <sensor_msgs/Joy.h>
void joydata ( const sensor_msgs::Joy& joy)
{
int sol;
int solcapraz;
int ileri;
sol = digitalRead(2);
solcapraz = digitalRead(4);
ileri = digitalRead(6);
if (joy.buttons[4] == 1 && joy.axes[0] >= 0.5 && joy.axes[1] >= 0.5)
{
digitalWrite(2, LOW);
sol = 0;
digitalWrite(6, LOW);
ileri = 0;
digitalWrite(4, HIGH);
}
else if (joy.axes[0] <= 0.5 && solcapraz == 1 && joy.axes[1] <= 0.5) {
digitalWrite(4, LOW);
solcapraz = 0;
}
else if (joy.buttons[4] >= 0.75 && joy.axes[0] >= 0.5)
{
digitalWrite(6, LOW);
ileri = 0;
digitalWrite(4, LOW);
solcapraz = 0;
digitalWrite(2, HIGH);
}
else if (joy.axes[0] <= 0.5 && sol == 1) {
digitalWrite(2, LOW);
sol = 0;
}
else if (joy.buttons[4] == 1 && joy.axes[1] >= 0.5 )
{
digitalWrite(2, LOW);
sol = 0 ...
Is there any particular reason why you subtract
digitalRead(2)
in your calls todigitalWrite()
?Also I notice the final
else
is unreachable yes?Thanks for your attention. I Sent my problem after review. Please look another comment. I didnt write there because number of characters was not enough for comment.