processing arduino connection difficulty - arduino

i am trying to send char's from processing to arduino, but arduino only recognizes 2 of them,
the problem is with the '2' char
when i press the 's' key the processing code is sending the '2' char because i can see the arduino rx led lighting but the motor does nothing,
with the '1' or '0' chars i have no problem,
i switched the '2' in the arduino code to correspond to drive_forward and then to drive_reverse, but the one that had '2' char assigned to it did not work in both cases,
as i said the '1'and '0' char are sent and received well
i guess it is something in the arduino code, but i don't know what
arduino code:
int motor1 = 4;
int motor2 = 5;
char val;
// --------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);
// Setup motors
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
}
// ---------------------------------------------------------------- Loop
void loop() {
if (Serial.available()>0)
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == '2'){
drive_forward();
}
if (val == '1'){
drive_reverse();
}
if (val == '0'){
motor_stop();
}
}
// --------------------------------------------------------------------------- Drive
void motor_stop(){
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
}
void drive_forward(){
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
delay(15);
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
delay(15);
}
void drive_reverse(){
digitalWrite(motor2, HIGH);
digitalWrite(motor1, LOW);
delay(15);
digitalWrite(motor2, LOW);
digitalWrite(motor1, LOW);
delay(15);
}
processing code:
import processing.serial.*;
Serial myPort;
void setup()
{
size(200,200);
myPort = new Serial(this, Serial.list()[2], 9600);
}
void draw() {
}
void keyPressed() {
if (key == 'w' || key == 'W')
{
myPort.write('1');
println("1");}
if (key == 's' || key == 'S')
{
myPort.write('2');
println("2");}
}
void keyReleased() {
myPort.write('0');
println("0");
}

As #tailedmouse said, send data as integer.
Processing code:
//skipped some code
void keyPressed() {
if (key == 'w' || key == 'W') {
myPort.write(1);
}
if (key == 's' || key == 'S') {
myPort.write(2);
println("2");
}
}
void keyReleased() {
myPort.write(0);
println("0");
}
Arduino code:
//skipped some code.
void loop() {
if (Serial.available()>0) {
// If data is available to
read, val = Serial.read();
// read it and store it in val
}
if (val == 2) {
drive_forward();
}
if (val == '1') {
drive_reverse();
}
if (val == '0') {
motor_stop();
}
}

Related

Communication between two hc 05 Bluetooth modules not working

I Have a problem with the connection between two hc 05 modules. The bluetooth modules are paired using AT commands. But when I run the code I cant get any data from the reciever. The bluetooth modules blink together but no data is displayed in the reciever. This is for a RC car that is controlled by a remote controller.
This is the code for the remote
void setup() {
pinMode(A6, INPUT);
pinMode(A5, INPUT);
pinMode(A4, INPUT);
pinMode(A2, INPUT);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
Serial.begin(38400);
}
void loop() {
int rud = analogRead(A6);
int rlr = analogRead(A5);
int lud = analogRead(A4);
int llr = analogRead(A3);
int led = digitalRead(3);
int obs = digitalRead(4);
int line = digitalRead(5);
Serial.println(lud);
Serial.println(llr);
Serial.println(led);
Serial.println(obs);
Serial.println(line);
Serial.print("\n");
if (lud <= 50) {
Serial.write('2');
delay(500);
}
else if (lud >= 950) {
Serial.write('1');
delay(500);
}
else if (llr <= 50) {
Serial.write('3');
delay(500);
}
else if (llr >= 950) {
Serial.write('4');
delay(500);
}
else if (led == 0) {
Serial.write('6');
delay(500);
}
else if (obs == 0) {
Serial.write('7');
delay(500);
}
else if (line == 0) {
Serial.write('8');
delay(500);
}
else if (llr > 50 && llr < 950 && lud > 50 && lud < 950){
Serial.write('5');
delay(500);
}
}
I have made sure all the baudrates are 38400 and when I just type the numbers in the serial monitor of the reciver it works fine. I have switched the bluetooth modules but It didn't work either. Boath remote and car has TX - RX and RX - TX wiring.
This is the code of the reciever
int fl = 9;
int fr = 10;
int bl = 11;
int br = 12;
int Max = 255;
char data = 0;
void setup() {
pinMode(fl, OUTPUT);
pinMode(fr, OUTPUT);
pinMode(bl, OUTPUT);
pinMode(br, OUTPUT);
Serial.begin(38400);
}
void forword() {
Serial.println("Foword");
analogWrite(fl, Max);
analogWrite(fr, 0);
analogWrite(bl, Max);
analogWrite(br, 0);
}
void backword() {
Serial.println("Backword");
analogWrite(fl, 0);
analogWrite(fr, Max);
analogWrite(bl, 0);
analogWrite(br, Max);
}
void left() {
Serial.println("Left");
analogWrite(fl, 0);
analogWrite(fr, Max);
analogWrite(bl, Max);
analogWrite(br, 0);
}
void right() {
Serial.println("Right");
analogWrite(fl, Max);
analogWrite(fr, 0);
analogWrite(bl, 0);
analogWrite(br, Max);
}
void Stop() {
Serial.println("Stop");
analogWrite(fl, 0);
analogWrite(fr, 0);
analogWrite(bl, 0);
analogWrite(br, 0);
}
void loop() {
if (Serial.available() > 0) {
data = Serial.read();
if (data == '1') {
forword();
}
else if (data == '2') {
backword();
}
else if (data == '3') {
right();
}
else if (data == '4') {
left();
}
else if (data == '5') {
Stop();
}}
}
I used softwere serial as well but It gave me the same result. The reciever is an arduino mega and the transmitter is an arduino nano
Pls help.
Thank you.
I have made sure all the valtages are correct and I have made a robot like this before and it worked but I just can't find what's wrong. I expect it to send data to the receiver with out any noise.

Arduino Uno and HC-05: Not showing any output on serial monitor

Arduino Uno - HC-05
Connections are: TX-RX, RX-TX, LED-D13, 5V-5V+
For this project, we can supply power to the Arduino through any +5V power source. You can use a USB port from your computer to power the Arduino, but in this project I used my laptop.
while (Serial.available()) is returning 0 and Serial.read() is returning -1.
Need Help!
Used Bluetooth voice recognition tool from playstore-"Arduino Voice Control"
#include <SoftwareSerial.h> //Replace (' ') with (< >)
SoftwareSerial BLU(0,1);
String voice;
int Green = 13; //Connect To Pin #13
//int Yellow = 2; //Connect To Pin #2
//int Red = 3; //Connect To Pin #3
void allon() {
//digitalWrite(Red, HIGH);
//digitalWrite(Yellow, HIGH);
Serial.print("start");
digitalWrite(Green, HIGH);
}
void alloff() {
//digitalWrite(Red, LOW);
//digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}
void setup() {
Serial.begin(9600);
BLU.begin(9600);
//pinMode(Red, OUTPUT);
//pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
}
void loop() {
//Serial.print("start loop");
//Serial.print(Serial.available());
while (Serial.available()) { //Check if there is an available byte to read
//Serial.print("start");
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
//Serial.print(Serial.read());
if (c == '#') {
break; //Exit the loop when the # is detected after the word
}
//Serial.print(c);
voice += c;
//Serial.print(voice+"\n");
}
if (voice.length() > 0) {
Serial.print("Start");
Serial.print(voice);
if (voice == "*turn on all LED") {
allon();
}
else if (voice == "*turn off all LED") {
alloff();
}
/*else if(voice == "*switch on red") {
digitalWrite(Red,HIGH);
}
else if(voice == "*switch on yellow") {
digitalWrite(Yellow,HIGH);
}*/
else if(voice == "*switch on green") {
digitalWrite(Green,HIGH);
}
/*else if(voice == "*switch off red") {
digitalWrite(Red,LOW);
}
else if(voice == "*switch off yellow") {
digitalWrite(Yellow,LOW);
}*/
else if(voice == "*switch off green") {
digitalWrite(Green,LOW);
}
voice=""; //Reset variable
}
}
You need to check for the app output first. If you already know it then mention that in comment otherwise do the following for printing app output first:-
const int LED = 5;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
while(Serial.available()>0){
switchstate = Serial.read();
Serial.print(switchstate); // First check what output are you getting from the application
Serial.print("\n");
delay(15);
if(switchstate == '1'){ // Compare your app output accordingly
digitalWrite(5, HIGH);
}
else if(switchstate == '0'){
digitalWrite(5, LOW);
}
}
}

Implementing the function interrupt with if statements

I want to implement the function interrupt () but I don't know exactly how..In this case there is 2 for loops which can be seen in the code:I want whenever one of the 2 buttons is pressed the process inside the loop to be interrupted immediately:
void loop() {
int brightButton = digitalRead(K1);
int ldrStatus = analogRead(ldrPin);
if (brightButton == LOW && ldrStatus >= 200)
{
for (int i = 0; i < 10; i++)
{
digitalWrite(greenLed, HIGH);
tone(buzzer,400);
delay(500);
noTone(buzzer);
delay(500);
}
}
else {
digitalWrite(greenLed, LOW);
}
int tempButton = digitalRead(K2);
int valNTC = analogRead(NTC);
if (tempButton == LOW && valNTC > 512)
{
for (int i = 0; i <10; i++)
{
digitalWrite(redLed, HIGH);
tone(buzzer,450);
delay(300);
noTone(buzzer);
delay(1000);
}
}
else {
digitalWrite(redLed, LOW);
}
}
Example code from the Arduino manual:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Note that this will interrupt the for loop and return to it once the interrupt service routine is finished.
If you want to abort the for loop check the pin state in every loop cycle and break if you want to leave the for loop or return if you want to leave loop().
Of course this is not "immediately".

How to merge two arduino programs on Bluetooth and Fingerprint?

Bluetooth and fingerprint are not responding simultaneously.
Bluetooth is used for controlling "Find me alarm" and Fingerprint is used to control the open and close motors.
The bluetooth is used to trigger the buzzer to make a buzzing sound. The fingerprint is used to control the four motors which is used to open and close the door
How to merge these two programs?
Fingerprint :
#include <FPS_GT511C3.h>
#include <SoftwareSerial.h>
FPS_GT511C3 fps(5, 4);
int buttonstate = 0;
void setup()
{
Serial.begin(9600);
delay(100);
fps.Open();
fps.SetLED(true);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(13,INPUT);
pinMode(16,OUTPUT);
pinMode(17,OUTPUT);
pinMode(18,OUTPUT);
pinMode(19,OUTPUT);
}
void loop()
{
buttonstate=digitalRead(13);
if (fps.IsPressFinger())
{
fps.CaptureFinger(false);
int id = fps.Identify1_N();
if (id <200)
{
Serial.print("Verified ID:");
Serial.println(id);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(1000);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(100);
digitalWrite(16,HIGH);
digitalWrite(17,LOW);
digitalWrite(18,HIGH);
digitalWrite(19,LOW);
}
else
{
Serial.println("Finger not found");
}
}
else
{
Serial.println("Please press finger");
}
if(buttonstate == HIGH)
{
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
delay(1000);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(100);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
delay(100);
}
Bluetooth :
#include<SoftwareSerial.h>
SoftwareSerial BT(14,15);
String readdatta;
void setup() {
BT.begin(9600);
Serial.begin(9600);
pinMode(12,OUTPUT);
}
void loop() {
char ch;
while(ch!=0)
{
ch=BT.read();
delay(100);
if(ch == '1')
{
digitalWrite(12,HIGH);
}
else
{
digitalWrite(12,HIGH);
}
}
ch = ' ';
}

Arduino program that allows me to change state through comm port?

new here! been recommended many times to come here for help so here I am.
I'm supposed to write a program that allows me to change the rate of a blinking LED light through the comm port. I'm sure this is easy but I've honestly got no clue as I am behind in this class.
anything would help really, i honestly want to learn how to do this, not just come here and get the answer.
thanks in advanced!
// global variables
#include <EEPROM.h>
unsigned long ms_runtime;
int state;
// possible values 0 -> 1 -> 2 -> 3 -> 0
int one_ms_timer;
// define all timers as unsigned long (they are incremented every 100ms = 0.1s)
unsigned long timer1;
unsigned long button_dbnc_tmr = 0;
// timer1 is used for blinking LED
const int LED1 = 13;
// function prototypes
void read_memory(void);
void update_memory(void);
void comm_control(void);
void led_control(void);
void turnoff(void);
void flash_1s(void);
void flash_2s(void);
void flash_3s(void);
void timers(void);
void setup()
{
read_memory();
pinMode(LED1, OUTPUT);
Serial.begin(9600);
//initialize uart
}
void loop()
{
static bool allow_change;
static int counter;
timers();
comm_control();
led_control();
}
void led_control()
{
switch (state)
{
case 0:
turnoff();
break;
case 1:
flash_1s();
break;
case 2:
flash_2s();
break;
case 3:
flash_3s();
break;
}
}
void turnoff()
{
digitalWrite(LED1, LOW);
}
void flash_1s()
{
if (timer1 < 10)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 20)
timer1 = 0;
}
}
void flash_2s()
{
if (timer1<20)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 30)
timer1 = 0;
}
}
void flash_3s()
{
if (timer1<30)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 40)
timer1 = 0;
}
}
void read_memory()
{
timer1 = EEPROM.read(one_ms_timer);
timer1++;
EEPROM.write(one_ms_timer, timer1);
Serial.begin(9600);
}
void update_memory()
{
EEPROM.update(timer1, one_ms_timer);
}
void comm_control(void);
{
char comm_reveier = serial_read;
if (
)
}
void timers(void)
{
if (millis() > (ms_runtime + 1))
{
ms_runtime = ms_runtime + 1;
one_ms_timer++;
}
else if (ms_runtime > millis())
ms_runtime = millis();
if (one_ms_timer > 99) //every 100 ms
{
one_ms_timer = 0;
button_dbnc_tmr++;
timer1++;
}
}
Load the Standard Firmata library on your Arduino board. Then use a library of your choice to build your comm prog. An overview of these can be found here.
Assuming you are using the Arduino IDE, this code sample should give you the general idea of what you need to do:
// pins for the LEDs:
const int ledPin = 13;
// Default blink rate
int rate = 1000;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(ledPin, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int rate = Serial.parseInt();
}
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(rate); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(rate); // wait for a second
}

Resources