XBee not communicating with Arduino connected - arduino

I have two XBee S2 modules. Both are communicating when I connect them directly to my computer and check via X-CTU terminal. The problem is when I try to send data wirelessly by connecting both of them with two Arduinos there is no communication at all. It doesn't send any value.
This is the code for the receiving side:
#include <SoftwareSerial.h>
SoftwareSerial XBSerial = SoftwareSerial(2, 3);
int BackMotorForward = 6;
int BackMotorReverse = 5;
int FrontMotorRight = 10;
int FrontMotorLeft = 9;
int sv1 = 0;
int sv2 = 0;
void setup ()
{
pinMode(BackMotorForward, OUTPUT); // Initialize the pin as an output.
pinMode(BackMotorReverse, OUTPUT); // Initialize the pin as an output.
pinMode(FrontMotorRight, OUTPUT); // Initialize the pin as an output.
pinMode(FrontMotorLeft, OUTPUT); // Initialize the pin as an output.
Serial.begin(9600);
// Set the data rate for the SoftwareSerial port
XBSerial.begin(9600);
// XBSerial.println(".");
}
void loop()
{
Serial.write(XBSerial.read());
if (XBSerial.available())
{
sv1 = XBSerial.read();
Serial.write(sv1);
}
if (XBSerial.available())
{
sv2 = XBSerial.read();
Serial.write(sv2);
}
if (sv1 < 280)
{
Serial.write("backward");
digitalWrite(BackMotorForward, HIGH);
digitalWrite(BackMotorReverse,LOW);
}
else if (sv1 > 380)
{
Serial.write("forward");
digitalWrite(BackMotorReverse,HIGH);
digitalWrite(BackMotorForward,LOW);
}
else
{
digitalWrite(BackMotorForward,LOW);
digitalWrite(BackMotorReverse,LOW);
}
if (sv2 > 380)
{
Serial.write("left");
digitalWrite(FrontMotorRight, HIGH);
digitalWrite(FrontMotorLeft,LOW);
}
else if (sv2 < 280)
{
Serial.write("right");
digitalWrite(FrontMotorLeft,HIGH);
digitalWrite(FrontMotorRight,LOW);
}
else
{
digitalWrite(FrontMotorRight,LOW);
digitalWrite(FrontMotorLeft,LOW);
}
}
This is the code for the sending side:
#include <SoftwareSerial.h>
SoftwareSerial XBSerial = SoftwareSerial(2, 3);
const int xpin = A0; // x-axis of the accelerometer
const int ypin = A1; // y-axis
void setup()
{
// Initialize the serial communications:
pinMode(xpin, INPUT); //x axis
pinMode(ypin, INPUT); //y axis
Serial.begin(9600);
Serial.println("testing");
// Set the data rate for the SoftwareSerial port
XBSerial.begin(9600);
XBSerial.println("testing!!!");
}
void loop()
{
// Print the sensor values:
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
// Print a tab between values:
Serial.print("\t");
Serial.println();
// Delay before next reading:
delay(100);
int val = analogRead(xpin);
int val2 = analogRead(ypin);
XBSerial.print(val); //Changed from write to print
XBSerial.print(val2);
}

Okay, it was a really stupid mistake. I was using softwareserial pins 2,3 for XBee, but instead I was connecting their pins directly to pin 0,1 (rx,tx) of the Arduino. That's the reason there was no communication.

Related

How to Send Float/Double over SPI Arduino (SAMD21 Based Board)

I am trying to send a double/float over SPI from my SAMD21 based board, with chip select on pin A1/A2. I have copied some code from the internet, but I don't really understand it, plus it only works for sending data between two Arduino Unos. Here is my master:
#include <SPI.h>
float a = 3.14159;
float b = 2.252332;
uint8_t storage [12];
float buff[2] = {a, b};
void setup()
{
digitalWrite(SS, HIGH);
SPI.begin();
Serial.begin(9600);
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
void loop()
{
digitalWrite(SS, LOW);
memcpy(storage, &buff, 8);
SPI.transfer(storage, sizeof storage ); //SPI library allows a user to
//transfer a whole array of bytes and you need to include the size of the
//array.
digitalWrite(SS, HIGH);
delay(1000);
}
And here is my slave:
#include <SPI.h>
byte storage [8];
volatile byte pos;
volatile boolean process;
float buff[2];
void setup()
{
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
SPCR |= _BV(SPIE);
pos = 0;
process = false;
Serial.begin(9600);
}
ISR(SPI_STC_vect)
{
byte gathered = SPDR;
if( pos < sizeof storage)
{
storage[pos++] = gathered;
}
else
process = true;
}
void loop()
{
if( process )
{
memcpy(buff,&storage,8);
Serial.print("This is val1:");Serial.println(buff[0], 5);
Serial.print("This is val2:");Serial.println(buff[1], 6);
storage[pos] = 0;
pos = 0;
process = false;
}
}
Any help would be aprreciated, and please understand that I am a newb in this subject.

How to control the display and non-display of sensor values ​in Arduino using Bluetooth?

I have a sensor that connects to the body and displays muscle signals.
In the setup guide of this sensor, it is said to upload the following code on Arduino, and when we open the Serial Monitor, the sensor values start to be displayed.
Now I want to control the display of these signals using Bluetooth.
So that when I click on the start button in my App, Serial.print() will start working. Also, when I click on the Stop button, the display of these signals and numbers will stop.
Sensor setup guide is this :
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
}
And this is how it works properly :
But when I upload a piece of code that I wrote to my Arduino, it only shows me just on value.
this is my code :
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
char Incoming_value = 0;
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
}
void loop() {
Incoming_value = Serial.read(); // "1" is for Start
if (Incoming_value == '1') {
Serial.println(Incoming_value);
StartSensor();
}
}
int StartSensor() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(200);
}
also please tell me How to write StopSensor Function for Stop print Sensor Value.
Try this code first (Without Bluetooth module)
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
char Incoming_value = 0;
int state = 0;
void setup() {
Serial.begin(9600);
//BTserial.begin(9600);
}
void loop() {
Incoming_value = Serial.read(); // "1" is for Start
if (Incoming_value == '1') {
state = 1;
}
else if (Incoming_value == '0') {
state = 0;
}
if (state == 1) {
StartSensor();
} else {
Serial.println(0);
}
}
int StartSensor() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(200);
}

Can I temporarily disable Arduino Serial data receive?

I am working on a project and I encountered some problems.
I am using a DHT11 temperature sensor, an Arduino Uno and a TFT LCD display 2.2-inch model ITDB02-2.2.
What I want my project to do is to use 2 functioning modes for the sensor that I can select from the keyboard at the beginning of the program(one which is normal and one which will be used on special occasions)(so I need serial communication).
I noticed that the screen does not function if I start a serial communication at any rate so I used Arduino Serial.begin(9600) and Serial.end() for the mode selecting part of the program.
THE PROBLEM: My Arduino is still sending data through serial port even if I ended the serial communication and is looking like this:
I found out that Serial.end() function does not shut off serial communication but just the rate of communication. I am curious if you have any idea that I can use in order to get rid of the extra data, to neglect it before the computer receives it.
I`m stuck. I thought that interruptions would be a solution but they are not as far as I researched on the internet.
My ARDUINO CODE:
#include <SimpleDHT.h>
#include <UTFT.h>
UTFT myGLCD(ITDB22,A5,A4,A3,A2);
SimpleDHT11 dht11;
// Declare which fonts we will be using
extern uint8_t BigFont[];
//dht sensor data pin
int dataPinSensor1 = 12;
char mode;
int del;
void setup()
{
Serial.begin(9600);
Serial.print("Select functioning mode");
mode=SensorModeSelect(mode);
Serial.end();
pinMode(12, INPUT);
}
void loop()
{
if(mode=='1') {
FirstFuncMode(dataPinSensor1);
}
if(mode=='2') {
SecondFuncMode(dataPinSensor1,del);
}
delay(10);
}
char SensorModeSelect(char in)
{
char mode='0';
while(mode=='0') {
if(Serial.available() > 0) {
mode=Serial.read();
}
}
if (mode == '1') {
Serial.print("\nMOD1 SELECTED: press t key to aquire data \n");
}
if (mode == '2') {
Serial.print("\nMOD2 SELECTED: press q if you want to quit auto mode \n");
Serial.print("Select the data aquisition period(not smaller than 1 second) \n");
}
return mode;
}
int DataAqPeriod()
{
int del=0;
while(del==0) {
while(Serial.available() > 0) {
//Get char and convert to int
char a = Serial.read();
int c = a-48;
del *= 10;
del += c;
delay(10);
}
}
del*=1000;
return del;
}
void FirstFuncMode(int dataPinSensor1)
{
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
bool DispCond=false;
Serial.begin(9600);
delay(1500);
if (Serial.read() == 't' ) {
DispCond=true;
//read temperature and compare it with an error value
if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("unreliable measurement or unselected functioning mode");
}
byte f = temperature * 1.8 + 32;
Serial.print((int)temperature);
Serial.print(" *C, ");
Serial.print((int)f);
Serial.print(" *F, ");
Serial.print((int)humidity);
Serial.println(" H humidity");
delay(1500);
}
Serial.end();
if(DispCond==true) {
//Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(BigFont);
//print value on LCD
displayNoInit((int)temperature,(int)humidity);
}
}
void SecondFuncMode(int dataPinSensor1,int del)
{
bool q=false;
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
Serial.begin(9600);
del=DataAqPeriod();
Serial.end();
//Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(BigFont);
while(q==false) {
Serial.begin(9600);
//read temperature and compare it with an error value
if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("unreliable measurement or unselected functioning mode \n");
}
float f = temperature * 1.8 + 32;
Serial.print((int)temperature);
Serial.print(" *C, ");
Serial.print((int)f);
Serial.print(" *F, ");
Serial.print((int)humidity);
Serial.println(" H humidity");
delay(del);
if(Serial.read() == 'q')
q=true;
Serial.end();
displayNoInit((int)temperature,(int)humidity);
delay(10);
}
}
void displayNoInit(int temperature,int humidity)
{
//effective data display
myGLCD.clrScr();
myGLCD.setColor(255, 255, 0);
myGLCD.setBackColor(10,10,10);
myGLCD.print(" Temperature ", CENTER, 10);
myGLCD.setColor(254, 254, 254);
myGLCD.printNumI(temperature, CENTER, 45);
myGLCD.setColor(255, 255, 0);
myGLCD.print("C ", RIGHT, 45);
myGLCD.print("Relative Hum.", CENTER, 90);
myGLCD.setColor(204, 245, 250);
myGLCD.printNumI(humidity, CENTER, 120);
myGLCD.print("%", RIGHT, 120);
}
You are correct in the definition that Serial.end() does not disable the serial monitor, only the interrupts. After calling Serial.end() you can disable the serial monitor like so.
#include <avr/io.h>
// Save status register, disable interrupts
uint8_t oldSREG = SREG;
cli();
// Disable TX and RX
cbi(UCSRB, RXEN);
cbi(UCSRB, TXEN);
// Disable RX ISR
cbi(UCSRB, RXCIE);
// Flush the internal buffer
Serial.flush();
// Restore status register
SREG = oldSREG;

How do I receive a HIGH or LOW signal from an IR sensor on an Arduino?

I'm trying to use an IR sensor with my Arduino Uno and only want a HIGH or LOW signal without decoding making any IR signal turn the state to a 1 or 0. There is also a motion sensor but that code has been removed.
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int relayPin = 4; //PIN FOR RELAY OPERATION
int irPin = 7; //IR Sensor pin
int lightState = 0;
int irVal = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(relayPin, OUTPUT);
pinMode(irPin, INPUT);
Serial.begin(9600);
}
void loop() {
irVal = digitalRead(irPin);
if (irVal == HIGH) {
lightState = 1;
Serial.println("IR received");
while(irVal == HIGH) {
irVal = digitalRead(irPin);
if(irVal == HIGH) {
irVal = LOW;
} else {
irVal = HIGH;
}
}
}
Are you trying to say that the input is not working correctly? Maybe try INPUT_PULLUP instead of INPUT in the setup loop.
For example:
pinMode(inputPin, INPUT_PULLUP);
Information about this principle you can find here:
https://www.arduino.cc/en/Tutorial/InputPullupSerial

Multiple If statements for Arduino

I'm new to this site and also the wonderful world of Arduino, I have been playing around with a Leonardo board and some Neopixel LEDS ( WS2812B ). I'm currently trying to set predefined colors on the LEDs with a single Pot, but also have an interrupt for a PIR sensor. I'm using the Fastled library since its great but at this point I seem to be stuck on the interrupt part. Any guidance will be appreciated !
#include "FastLED.h"
#define NUM_LEDS 3
#define DATA_PIN 6
#define PIR_PIN 2
int PIR_PIN = 0
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
CRGB leds[NUM_LEDS];
void setup() {
// initialize the LED pin as an output:
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// initialize serial communications:
pinMode(PIR_PIN, INPUT);
attachInterrupt(0, PIR_PIN, CHANGE);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
if (analogPin < 1000)
{
leds[0] = CRGB::Red;
FastLED.show();// do Thing A
}
else if (analogPin < 500)
{
leds[0] = CRGB::Orange;
FastLED.show();// do Thing B
}
else if (analogPin < 250)
{
leds[0] = CRGB::Green;
FastLED.show();//do fuckity
}
else
{
leds[0] = CRGB::Purple;
FastLED.show();// do Thing C
}
}
void PIN_PIR () {
buttonState = digitalRead(PIR_PIN);
digitalWrite(DATA_PIN, buttonState);
}

Resources