no matching function for call to LiquidCrystal::setCursor(int) - arduino

So, i was trying to make the cursor move on an lcd with a joystick but,
I type this:
int iniCursorX=6;
int iniCursorY=2;
#include <LiquidCrystal.h> //lcd
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //setting up lcd
// Arduino pin numbers
const int SW_pin = 6; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
int Y_pin = 1; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
lcd.print("Hello!");
analogWrite(Y_pin, INPUT);
}
void loop() {
lcd.setCursor(iniCursorX,iniCursorY);
lcd.cursor();
delay(200);
lcd.noCursor();
delay(200);
if(Y_pin=0, iniCursorX >= 2) {
int j=iniCursorX;
lcd.setCursor(j-1);
}
}
Then I Get this:
no matching function for call to LiquidCrystal::setCursor(int)

At the bottom of your code you have:
lcd.setCursor(j-1);
but lcd.setCursor takes two arguments.
Change it to something like lcd.setCursor(0, j-1); or anything you need.

Related

Arduino Automatic Clothes Line : How can I make my meet the different criteria?

So I'm a novice in programming and is helping my friend with coding an automatic clothes line for a project.
Each component works fine individually but when I put all the code together the motors are confused in which direction to go. can anyone take a look at it and give us feedback?
We have a rain sensor, light sensor, 2 switches , and a motor.
When it rains OR when it is dark the motors pulls the clothes in and when the the clothes reaches the end it touches switch 1 and stops the motors.
When it stop rains/sunny AND/OR when there light it pulls the clothes out in the open.
Here is the code I was provided:
**
#include <Stepper.h>
//motors
const int stepsPerRevolution = 400;
//motor speeds
Stepper myStepper = Stepper(stepsPerRevolution, 2,4, 3, 5);//clockwise
Stepper myStepper2 = Stepper(stepsPerRevolution, 5, 3, 4, 2);//counterclockwise
//rain sensor
const int capture_D =4;
const int capture_A = A0;
int val_analogique;
int led = 8;
//switches
const int switch1 = A3;
const int switch2 =A4;
void setup() {
// put your setup code here, to run once:
// motors
myStepper.setSpeed(60);
myStepper2.setSpeed(60);
//rain sensor
pinMode(capture_D, INPUT);
pinMode(capture_A, INPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//light sensors
unsigned int AnalogValueL;
AnalogValueL = analogRead(A5);
Serial.println(AnalogValueL);
delay(2000);
//rainsensor
if((digitalRead(capture_A) && digitalRead(capture_D)) ==LOW)
{
digitalWrite(led, HIGH);
myStepper2.step(stepsPerRevolution); // bringclothesout
Serial.println("DRY");
delay(1000);
}
else
{
//when it rains the clolthes re brought in
digitalWrite(led, LOW);
Serial.println("WET");
myStepper.step(stepsPerRevolution);//bring clothes in
delay(1000);
}
if(AnalogValueL < 1010)
{
digitalWrite(led, HIGH);
Serial.println("SUNNY");
myStepper2.step(stepsPerRevolution);
delay(1000);
}
else
{
digitalWrite(led, LOW);
Serial.println("Dark");
myStepper.step(stepsPerRevolution);
delay(1000);
}
//switches
unsigned int AnalogValueS;
AnalogValueS = analogRead(switch1);
Serial.println(AnalogValueS);
delay(2000);
if(AnalogValueS <= 1000)
{
digitalWrite(led, HIGH);
Serial.println("Off");
myStepper.setSpeed(0);
}
else
{
digitalWrite(led, LOW);
Serial.println("on");
}
unsigned int AnalogValueSS;
AnalogValueSS = analogRead(switch2);
Serial.println(AnalogValueSS);
delay(2000);
if(AnalogValueSS <= 1000)
{
digitalWrite(led, HIGH);
Serial.println("ON");
}
else
{
digitalWrite(led, LOW);
Serial.println("Off");
}
*/
}//end of program
**
Feedback?
Here's my feedback:
Your code needs a little bit of tidying up for the sake of clarity and readability.
It is always recommended to declare your Serial.begin(); at the beginning of the setup() function.
You forgot to declare the switches pins as inputs:
pinMode(switchX, INPUT); // where X = 1, 2
Why are you using analog pins as switch inputs? (const int switch1 = A3). If you are connecting ON/OFF switches here you should rather use digital pins. In case your switches are the light sensors, then they are fine (read comment 9 below). Otherwise, this:
AnalogValueS = analogRead(switch1);
is wrong as you cannot read a digital switch with an analogRead()
Additionally, you don't say if your switches are active HIGH or active LOW, namely, if they pulse LOW when pressed and HIGH when released or viceversa.
One important mistake is that you are assigning pin 4 twice for different functions. First as a motor pin, then as capture_D:
Stepper myStepper = Stepper(stepsPerRevolution, 2, 4, 3, 5);
Stepper myStepper2 = Stepper(stepsPerRevolution, 5, 3, 4, 2);
const int capture_D = 4;
You read the analog value in a variable declared inside the main() loop while you previously declared int val_analogique without using it. This is poor consistency.
Similarly, you read from analog pin A5 without previously declaring it as an INPUT:
AnalogValueL = analogRead(A5);
The flow of your loop could be affected by that many delay() throughout the code.
You expressed having three sensors:
We have a rain sensor, light sensor, 2 switches, and a motor.
It is good practice to label the pins from these sensors with a representative name from the sensor in question: e.g.:
const int lightSensor1 = 4;
In the C++ mindset is common practice to use enums to define states from a specific event or scenario. For instance:
typedef enum {
RAINING,
DARK,
NOT_RAINING,
SUNNY
}WEATHER_CONDITIONS;
WEATHER_CONDITIONS currentCondition = SUNNY;
where the typedef means that you are creating your own data type WEATHER_CONDITIONS and their assigned values are 0,1, 2,... according to their order.
With regards to the Stepper.h library you are declaring two Stepper instances expecting that in that way you will reverse the direction of the motor. That is NOT how it works. According to the library (Stepper.cpp) to reverse the motor, you need to send a negative value as an argument to reverse its direction. Please read line 184 from this file from the library.
On this line:
if((digitalRead(capture_A) && digitalRead(capture_D)) ==LOW)
you are attempting to read a digital value from an analog pin. While it might work if the voltage on that pin is at a logic level, this is not recommended.
Finally, you SHOULD connect the switches for stopping the motor to INTERRUPT pins. This is a more effective way to detect when the switch has been pressed and take immediate action by changing a state. You would have to use pins 2 and 3 (INT0 and INT1).
This is my take on this code. Compiled correctly but UNTESTED!
#include <Arduino.h>
#include <Stepper.h>
/*Constants*/
//motor speeds
const int stepsPerRevolution = 400;
/*Pinout declarations*/
// Motor pins
const int motorPins[4] = {2, 3, 4, 5};
//rain sensor
const int capture_D = 4; // capture_D
const int capture_A = A0; // A0
const int lightSensor = A5;
//switches
const int switch1 = 9; // Previously: A3 So this is an analog switch?
const int switch2 = 10; // Previously: A4 You should assing this to other two digital pins
//Led
const int led = 8;
/*Custom variables*/
typedef enum {
DARK,
SUNNY
} LIGHT_CONDITIONS;
LIGHT_CONDITIONS lightState = SUNNY;
const char* lightLabels[] = {"DARK",
"SUNNY"};
typedef enum {
DRY,
WET
} HUMIDITY_CONDITIONS;
HUMIDITY_CONDITIONS humidityState = DRY;
const char* humidityLabels[] = {"DRY",
"WET"};
typedef enum {
CW, // Let's assume that this is bring them in
CCW, // this is take them out
STOP
} MOTOR_DIRECTION;
MOTOR_DIRECTION currentDirection = STOP; // This declares a default state
const char* directionLabels[] = {"Clothes In",
"Clothes Out"};
/*General variables*/
int rawLightValue;
// Objects declaration
Stepper myStepper = Stepper(stepsPerRevolution,
motorPins[0],
motorPins[1],
motorPins[2],
motorPins[3]);
void activateMotor(int direction) {
switch (direction) {
case MOTOR_DIRECTION::CW:
myStepper.step(-stepsPerRevolution);
currentDirection = CW;
break;
case MOTOR_DIRECTION::CCW:
myStepper.step(stepsPerRevolution);
currentDirection = CCW;
break;
case MOTOR_DIRECTION::STOP:
currentDirection = STOP;
myStepper.step(0);
break;
default:
// so much empty
break;
}
}
void setup() {
Serial.begin(9600);
// motors
myStepper.setSpeed(60);
//rain sensor
pinMode(capture_D, INPUT);
pinMode(capture_A, INPUT);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(lightSensor, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
// First, read light sensor
rawLightValue = analogRead(lightSensor);
Serial.println(rawLightValue);
if (rawLightValue < 1010) {
digitalWrite(led, HIGH);
lightState = SUNNY;
Serial.println(lightLabels[lightState]);
} else {
digitalWrite(led, LOW);
lightState = DARK;
Serial.println(lightLabels[lightState]);
}
// Then read rain sensor --> digital portion only, granted that you have calibrated
// its pulsing voltage corectly. I am assuming that you are using one of these sensors:
// https://circuitdigest.com/microcontroller-projects/rain-detector-using-arduino
if (digitalRead(capture_D) == LOW) {
digitalWrite(led, HIGH);
humidityState = DRY;
Serial.println(humidityLabels[humidityState]);
}
else {
//when it rains the clolthes re brought in
digitalWrite(led, LOW);
humidityState = WET;
Serial.println(humidityLabels[humidityState]);
}
// Based on these conditions, activate the motor accordingly
/* When it rains OR when it is dark the motors pulls the clothes in and when
the the clothes reaches the end it touches switch 1 and stops the motors.
*/
if ((humidityState == WET) || (lightState == DARK)) {
// While the switch is released, meaning that the switch has NOT been pressed:
while (digitalRead(switch1) == HIGH) {
activateMotor(CCW);
Serial.println(directionLabels[currentDirection]);
}
// Finally, let the motors go until the swithces tell the system to stop the motor
activateMotor(STOP);
}
if ((humidityState == DRY) && (lightState == SUNNY)) {
// While the switch is released, meaning that the switch has NOT been pressed:
while (digitalRead(switch2) == HIGH) {
activateMotor(CW);
Serial.println(directionLabels[currentDirection]);
}
// Finally, let the motors go until the swithces tell the system to stop the motor
activateMotor(STOP);
}
}

LCD doesn't work in a simulated enviroment

I'm using Tinkercad, and since it's my first time programming an LCD I just copied the procedure to connect the pins and make it work.
The thing is that it just lights up without displaying anything, I tried both wiring and unwiring the R/W pin but that doesn't work either, nothing will be displayed.
What did I miss? The other functions of the code works normally.
Image of the circuit:
This is the code:
#include <LiquidCrystal.h>
const int pin = 0; // analog pin
float celsius = 0, farhenheit =0; // temperature variables
float millivolts; //Millivolts from the sensor
int sensor;
const int G_LED = 13;
const int Y_LED = 12;
LiquidCrystal lcd(10, 9, 5, 4, 3, 2); // Building the LCD
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("C="); // "C=", "F=" and "mV" should be printed
lcd.setCursor(0, 1); // on the LCD in a column
lcd.print("F=");
lcd.setCursor(0, 2);
lcd.print("mV=");
pinMode(G_LED, OUTPUT);
pinMode(Y_LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensor = analogRead(pin); // Reading the value from the LM35 sensor using the A0 ingress
millivolts = (sensor / 1023.0) * 5000; // Converting the value in a number that indicates the millivolts
celsius = ((sensor * 0.00488) - 0.5) / 0.01; // Celsius value (10 mV for each degree, 0°=500mV)
farhenheit = celsius * 1.8 + 32; // Fahrenheit value
lcd.setCursor(4, 2); // Set the cursor at the right of "mV="
lcd.print(millivolts); // Print the mV value
lcd.setCursor(4, 0); // Same here for °C and °F
lcd.print(celsius);
lcd.setCursor(4, 1);
Serial.print(farhenheit);
if (millivolts < 700) { // Green LED is on when the temperature is under or equal to 20°
// if (celsius < 20) { // Alternative
analogWrite(G_LED, 255);
analogWrite(Y_LED, 0); }
else {
analogWrite(G_LED, 0);
analogWrite(Y_LED, 255); // Yellow LED is on when the temperature is above of 20°C
}
delay(1000);
}
Fix - I could not find the error, but I suspect it was due to the strange layout of the connections. You also tried to set the cursor to line 3, but the LCD you were using did not have 3 lines, it was a 16x2 LCD.
What I Did - So what I did was I re-did the entire project, I linked up a new LCD, this time with a digital contrast so that it could be dynamically changed. I also made sure to include the sensor you used in your last project. Over-all the project is an Arduino controlling an LCD and outputting the temperature in Fahrenheit and millivolts.
Here is the project link (Tinkercad).
Code:
#include <LiquidCrystal.h>
// Adds the liquid crystal lib
int contrast = 40; // Set the contrast of the LCD
LiquidCrystal lcd (12, 11, 5, 4, 3, 2); // Instantiate the LCD
float fahrenheit;
float millivolts;
void setup ()
{
analogWrite(6, contrast); // Wrjte the contrast to the LCD
lcd.begin(16, 2); // Init the LCD
}
void loop ()
{
int sensor = analogRead(0);
millivolts = (sensor/1023.0)*5000;
fahrenheit = (((sensor*0.00488)-0.5)/0.01)*1.8+32;
lcd.setCursor(0, 0);
lcd.print("Temp(F):");
lcd.setCursor(11, 0);
lcd.print(fahrenheit);
lcd.setCursor(0, 1);
lcd.print("Volts(mV):");
lcd.setCursor(12, 1);
lcd.print(millivolts);
}
Diagram

I can't fully understand why the program isn't working - LCD Screen problems and more

I'm working on a project for an automatic Malteser (or similar product) dispenser and I got the code to work, but after leaving it it for a bit, the screen played up and it won't work properly. The LED also is always on and it can seem to move on from the Setup
I have tried:
searching the code for faults
checking the wiring
redoing the wiring in case i missed something
Code:
#include <Servo.h>
#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>
DS3231 Clock;
bool Century = false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
int second, minute, hour, date, month, year, temp;
int button = 8;
int maltesersEaten = 0;
int lastEatenSe = 0;
int lastEatenMi = 0;
int lastEatenHo = 0;
int lastEatenDa = 0;
int lastEatenMo = 0;
int lastEatenYe = 0;
bool eat;
int ledPin = 10;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myServo;
byte degree[8] = {
B00100,
B01010,
B00100,
B00000,
B00000,
B00000,
B00000,
};
void setup() {
**lcd.createChar(0, degree);
lcd.begin(16, 2);
// Print a message to the LCD
lcd.print("INITIALIZING");
// Start the I2C interface
Wire.begin();
// Start the serial interface
Serial.begin(9600);
pinMode(button, INPUT);
myServo.attach(9);**
myServo.write(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(1000);
lcd.clear();
}
void getData() {
second = Clock.getSecond();
minute = Clock.getMinute();
hour = Clock.getHour(h12, PM);
date = Clock.getDate();
month = Clock.getMonth(Century), DEC;
year = Clock.getYear();
temp = Clock.getTemperature();
lcd.setCursor(0, 0);
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);
lcd.setCursor(0, 1);
lcd.print(date);
lcd.print(",");
lcd.print(month);
lcd.print(",");
lcd.print(year);
lcd.setCursor(9, 0);
lcd.print(temp);
lcd.write(byte(0));
lcd.print("C");
lcd.setCursor(9,1);
lcd.print("ATE:");
lcd.print(maltesersEaten);
}
void mE() {
maltesersEaten = (maltesersEaten + 1);
lastEatenSe = second;
lastEatenMi = minute;
lastEatenHo = hour;
lastEatenDa = date;
lastEatenMo = month;
lastEatenYe = year;
}
void check() {
lcd.clear();
lcd.print("-Last Malteser-");
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);
lcd.setCursor(0, 1);
lcd.print("Eaten: ");
lcd.print(maltesersEaten);
delay(5000);
lcd.clear();
}
void loop() {
if (digitalRead(button) == LOW) {
digitalWrite(ledPin, HIGH);
myServo.write(170);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("---- Have A ----");
lcd.setCursor(0, 1);
lcd.print(" Malteser ");
delay(5000);
myServo.write(10);
lcd.clear();
check();
mE();
digitalWrite(ledPin, LOW);
}
else if (digitalRead(button) == HIGH) {
getData();
}
}
The desired effect would be the screen produces a message, the LED flashes, then it shows the time, date and temperature of the room, as well as how many Maltesers have been eaten. then when the button is pressed, the servo moves, the LED turns on and the LCD changes through a few different screens, then it returns to the time, date, temp, and number of Maltesers eaten. However the actual result is that the LED turns on permanently, the LCD produces two solid blocks and nothing else, and the servo goes limp, when I press the button nothing changes. I think the problem is in the setup if that is of nay help.
In the program you call the function: check(). In there is a five second delay and in the loop there is another 5s delay. Could it be that you always press the button when the delay is on. Then the program would not notice that the button was pressed and continues with the loop again. This would also explain why the led won't turn off because it is turned on again right away.
To test if this is the case just hold the button down for at least 10s and sea if it works.
If this is the case, to fix this problem try working with an interrupt.
See documentation here: https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

Arduino 16*2 LCD displaying random characters instead of expected text

I'm trying to display some data to my 16*2 LCD module but there are some random characters being shown. I have some simple code that I used to test my LCD display and it works perfectly. Code:
#include<LiquidCrystal.h>
// initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
void setup() {
lcd.begin(16, 2);
}
void loop() {
lcd.print("Testing"); // thats the top row string
delay(1800);
lcd.setCursor(2, 1); // move to the 2nd row, 1st col
lcd.print("Display this!");
delay(1800);
lcd.clear();
lcd.setCursor(7, 1);
delay(400);
lcd.blink();
lcd.setCursor(6, 1);
delay(400);
lcd.setCursor(5, 1);
delay(400);
lcd.setCursor(4, 1);
delay(400);
lcd.setCursor(3, 1);
delay(400);
lcd.setCursor(2, 1);
delay(400);
lcd.setCursor(1, 1);
delay(400);
lcd.setCursor(0, 1);
lcd.noBlink();
lcd.print("Silly Isn't It?");
lcd.cursor();
delay(1800);
lcd.noCursor();
lcd.clear();
}
However, I have more things on the breadboard now - the LCD, micro SD reader, potentiometer and an LM35 temperature sensor
and this my code:
#include<LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>
////////// LCD
//initializing pins - RS, E, rest of data pins
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
const int CS_PIN = 10;
const int POW_PIN = 8;
int refreshRate = 2000; // for reading vals
////////// LEDs
int ledPinR = 11;
int ledPinG = 12;
int ledPinY = 13;
////////// LM35
float temp;
int tempPin = A0;
void setup() {
////////// LED
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinY, OUTPUT);
////////// LCD
lcd.begin(16, 2);
lcd.print("please wait..."); //thats the top row string
delay(2000);
lcd.clear();
lcd.blink();
////////// SD
Serial.begin(9600);
Serial.println("\nNow Initializing SD card...");
pinMode(CS_PIN, OUTPUT);
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if(!SD.begin(CS_PIN)){
Serial.println("\nSomething went wrong. Probably card failure, card format, or something else.");
return;
}
Serial.println("\nCard ready!");
File commandFile = SD.open("tempLevels.txt");
if(commandFile){
Serial.println("\nNow Reading Command File...");
while(commandFile.available())
{
refreshRate = commandFile.parseInt();
}
Serial.print("\nTapiwa, the refresh rate is: ");
Serial.print(refreshRate);
Serial.print(" ms");
commandFile.close();
}
else{
Serial.println("Oops! Failing to read command file!");
return;
}
}
void loop() {
////////// LM35
temp = analogRead(tempPin);
float mV = (temp / 1024.0) * 5000;
float tempVal = mV / 10;
Serial.println("\nTemperature is: ");
Serial.println(tempVal);
File dataFile = SD.open("log.csv", FILE_WRITE); // dont know about that .csv format
if(dataFile)
{
dataFile.print("\nTemperature is: ");
dataFile.print(tempVal);
dataFile.println("Deg");
dataFile.close();
Serial.println("\nSaved in DataFile >> Temperature is: ");
Serial.print(tempVal);
}
else
{
Serial.println("DataFile error! Reading not saved");
Serial.println("Could not open log file! Not on SD card!");
}
lcd.print("Temp: ");
lcd.setCursor(2, 1); // 2nd row, 1st col
lcd.print(tempVal);
delay(2000);
lcd.clear();
delay(refreshRate);
}
I'm getting the results in the serial monitor but the LCD displays random characters which resemble encrypted text. Where did I go wrong?
I've looked at at multiple posts on this site and other sites but they are not that useful:
This one made sense but not useful in my case.
This one too!.
And this one
If you take a look at the documentation of Serial, it says:
All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.
Thus, you should rearrange your scheme so that the LCD doesn't use pin 1.

XBee not communicating with Arduino connected

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.

Resources