I must send 3 values gave by 3 potentiometers ,connected by an Arduino Uno, and send them to another Arduino Uno whith a serial comunication. The received values must be distributed in 3 servo motors so that each knob able to control the servo motor movement.
the problem with this program is that the values received are not distributed correctly (for example the case that the value of the potentiometer 1 is to be read by the servo motor 3 or other cases). I ask if I could help to synchronize data received with the distribution of them to the servo motors. Thanks in advance.
sketch arduino with potentiometers:
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255); /* synch symbol */
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
sketch arduino with servo motors:
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
if (BTserial.available() > 0) {
if (BTserial.available() == 255) { /* synch */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}
master code( no modificated):
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255);
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
slave code( modificated):
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
// Read serial input:
if (BTserial.available() > 0) {
byte synch_symbol = BTserial.read();
if (synch_symbol == 255) {
while (BTserial.available() < 3) { }; /* wait for values */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
/* do something with values */
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}
Related
I need to send orders from Arduino to ESP32.
I have one joystick button to test.
Arduino nano is sender
Esp32 is receiver
Esp32 receives the joystick button information from Arduino (each time I push the button).
I need the Esp32 to Serial.write according to the data, for example:
If I press the button in Arduino: Send the data to Esp32 and turn bluetooth on (or turn a led on).
These are my codes:
//Arduino NANO sender
byte j = 45;
#define boton_joystick A1
void setup() {
Serial.begin(9600);
pinMode(boton_joystick, INPUT_PULLUP);
}
int boton_joystick_state;
void loop() {
//Serial.println("100");
//Serial.write("BOTON EN GRANDE");
//delay(1500);
if(!digitalRead(boton_joystick)) {
boton_joystick_state = 1;
delay(170);
} else {
boton_joystick_state = 0;
}
if (boton_joystick_state) {
Serial.println(j);
Serial.print(" ");
Serial.write(j);
Serial.println();
}
//ESP-32 receiver
#define RXD2 16
#define TXD2 17
byte j = 45;
int comdata;
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop() {
//Serial.print("LEYENDO ARDUINO");
Serial.println(Serial2.readString());
if (Serial2.available() >0) {
char comdata = char(Serial2.read());
if (comdata == 'j') {
Serial.println("joystick activado");
}
}
}
am not sure
but am using Nano 33 BLE with UART and Nano has also Serial1 no need to serial2 no need to Softwearserial. Sensd on serial 1 and recive in Serial 1 but also you have to connect it Via USB. so your serial is USB and your serial 1 is TX RX.
for me it work so you can try it.
I have to do a project for Uni using 2 arduino uno, 2 HC 05 bluetooth module and 2 sensors for each arduino. I have set a module to be the slave, and the master connects to the slave only.
When I am trying to send the data from slave to be read by master, it keeps reading 0.
I am using Arduino IDE, I have no errors and everything runs.
This is the code I have so far.
Also, I want to send data from 2 sensors from the slave to the master, but I do not know how to do that.
Slave code
#include <SoftwareSerial.h>
#define TILT 7 // tilt sensor pin
#define LDR 8 //light intensity sensor pin
#define rxPin 10
#define txPin 11
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
int sentBytes; ////// SLAVE CODE
byte data[2];
void setup() {
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(TILT, INPUT);
pinMode(LDR, INPUT);
Serial.begin(9600);
nodeCommunication.begin(9600);
delay(2000);
}
void loop() {
//int LDRval = analogRead(LDR); // light intensity sensor (candela)
//int Tilt_Sensed = digitalRead(TILT);
data[0] = analogRead(LDR);
data[1] = digitalRead(TILT);
if(nodeCommunication.available()){
sentBytes = nodeCommunication.write(data[0]);
Serial.println("I reached this if");
}
Serial.println(analogRead(LDR));
Serial.println(sentBytes);
delay(2000);
}
Master Code
#include "dht.h"
#include <SoftwareSerial.h>
#define dht_apin A0 // Analog Pin sensor is connected to A0
dht DHT; // DHT.humidity // DHT.
#define rxPin 10
#define txPin 11
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
byte data = 1;
///// MASTER CODE
void setup(){
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
delay(2000);//Wait before accessing Sensor
}//end "setup()"
void loop(){
//Start of Program
DHT.read11(dht_apin); // reading the data from the sensor
if(nodeCommunication.available()){
data = nodeCommunication.read();
Serial.println("I reached this if");
}
Serial.print(data); Serial.println(" candela;");
Serial.print(DHT.temperature); Serial.println(" C");
Serial.print(DHT.humidity); Serial.println(" % humidity");
delay(2000);//Wait 2 seconds before accessing sensor again.
//Fastest should be once every two seconds.
}// end loop()
Please tell me what I am doing wrong. Thank you so much!
byte data = 1; - should be just byte data;
Have you checked the TX/RX connections? The TX from the master has to connect to the RX of the slave and visa versa. Als GND has to be connected to eachother.
After that, let the master (the transmitter) just Serial.write (in your case nodeCommunication.write() ) to the slave (the receiver).
So something like this:
Master:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
byte data = 0x01; //Hex value for ''1''
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
}
void loop()
{
nodeCommunication.write(data); //Sends byte data to slave
delay(1000);
}
Slave:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
byte incoming
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
void setup()
{
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600);
}
void loop()
{
if(nodeCommunication.available > 0) //Only if data is available
{
byte incoming = nodeCommunication.read(); //read byte from master
Serial.println("Incoming = ");
Serial.println(incoming);
}
else
{
Serial.println("No data available....");
}
}
After your Serial communication is established, implementing the DHT value will be the next step.
I have a temperature sensor set up working with an LCD and a stick to adjust the brightness. I now want the temperature sensor to send a text whenever it reaches a certain temperature. Can somebody please help.
The GSM unit i have is the SIM800L
below is what I have so far :
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensor=A1; // Assigning Analog Pin A1 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahrenheit
float vout; //temporary variable to hold sensor reading
void setup()
{
pinMode(sensor,INPUT); // Configuring pin A1 as INPUT Pin
Serial.begin(9600);
lcd.begin(16,2);
delay(500);
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
}
you could use the library Fona to send SMS with a Sim800L
to send a message you use the command -> fona.sendSMS(sendto, message)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
fonaSS.begin(9600);
if (!fona.begin(fonaSS)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
Serial.println(F("FONA is OK"));
char sendto[21], message[141];
:
:
//initialize sendto and message
:
:
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
to adapt the program to your case: i have put some lines of codes from setup to loop (easy to understant sendto and message definitions in loop now)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
fonaSS.begin(9600);
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
another way to send sms
you could test the hayes command: for example
void sendsms(){
Serial.println("Sending text message...");
fonaSS.print("AT+CMGF=1\r"); // SMS MODE
delay(100);
// phone number
fonaSS.print("AT+CMGS=\"+33676171212\"\r"); //indicate your phone number
delay(100);
// message here
fonaSS.print("Message test \r");
// CTR+Z in mode ASCII, to indicate the end of message
fonaSS.print(char(26));
delay(100);
fonaSS.println();
Serial.println("Text send");
}
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
I want to use bluetooth connection to control, how long something is on. So I read the Serial Input. If it is a number, I take the number and put it into a delay. After it is through, I write something and check again. If the Serial Read is not a number, it should turn off. in The problem is, the led keeps running. Do you see, what is my mistake?
#include <SoftwareSerial.h>
#define ledPin 13
#define rxPin 10
#define txPin 11
SoftwareSerial btSerial(rxPin, txPin);
int btData;
void setup() {
btSerial.begin(9600);
btSerial.println("bluetooth available");
pinMode(ledPin,OUTPUT);
serv.attach(3);
serv2.attach(5);
}
void loop() {
if (btSerial.available()){
btData = btSerial.read();
if(isDigit(btData)){
digitalWrite(ledPin,1);
btSerial.println("LED on Pin is on");
delay(btData*10);
}
else {
digitalWrite(ledPin,0);
btSerial.println("LED on Pin is off");
}
}
delay(100);
}
Try to use this:
#include <SoftwareSerial.h>
#define ledPin 13
#define rxPin 10
#define txPin 11
SoftwareSerial btSerial(rxPin, txPin);
int btData;
void setup() {
btSerial.begin(9600);
btSerial.println("bluetooth available");
pinMode(ledPin,OUTPUT);
serv.attach(3);
serv2.attach(5);
}
void loop() {
if (btSerial.available())
btData = btSerial.read();
if(isDigit(btData)){
digitalWrite(ledPin,1);
btSerial.println("LED on Pin is on");
delay(btData*10);
}
else {
digitalWrite(ledPin,0);
btSerial.println("LED on Pin is off");
}
delay(100);
}
And the reason is because the enabling and disabling was done only when there is serial data and if you put the if anywhere else it would be checked always.
I'm trying to code a GSM caller number receiver. When I (as the phone answerer) answer the phone it should print out the caller number.
I have trouble finding the right AT command for receiving the caller number. I tried AT+CLIP=1\r and on the loop +CLIP, but with no success.
Here is my code:
#include <GSMSim.h>
#include <SoftwareSerial.h>
#define RX 7
#define TX 8
#define RESET 2
#define BAUD 9600
GSMSim gsm;
SoftwareSerial mySerial = SoftwareSerial(RX, TX);
/*
* Also you can this types:
* GSMSim gsm(RX, TX);
* GSMSim gsm(RX, TX, RESET);
* GSMSim gsm(RX, TX, RESET, LED_PIN, LED_FLAG);
*/
void setup() {
Serial.begin(9600);
Serial.println("GSMSim Library - Call Example");
Serial.println("");
delay(1000);
gsm.start(); // baud default 9600
mySerial.read();
mySerial.print("AT+CLIP=1\r");
}
void loop() {
Serial.println(gsm.callStatus());
gsm.callAnswer();
Serial.println("Number:");
Serial.println(mySerial.print("+CLIP"));
delay(1000);
}
I got It working by using mySerial, (ATDevice) read function and using command function to accualy print it out, anyone who looks up how it work, here Is my code
#include <GSMSim.h>
#include <SoftwareSerial.h>
#include <string.h>
#define RX 7
#define TX 8
#define RESET 2
#define BAUD 9600
char outArray;
char inData[20];
char inChar=-1;
byte index = 0;
char * pch;
char* substring(char*, int, int);
GSMSim gsm;
SoftwareSerial ATDevice = SoftwareSerial(RX, TX);
/*
* Also you can this types:
* GSMSim gsm(RX, TX);
* GSMSim gsm(RX, TX, RESET);
* GSMSim gsm(RX, TX, RESET, LED_PIN, LED_FLAG);
*/
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ATDevice.begin(9600);
command("AT+CLIP=1",1000);
delay(1000);
}
String command(const char *toSend, unsigned long milliseconds) {
String result;
ATDevice.println(toSend);
unsigned long startTime = millis();
Serial.print("Return: ");
while (millis() - startTime < milliseconds) {
if (ATDevice.available()) {
char c = ATDevice.read();
result += c; // append to the result string
}
}
Serial.println(); // new line after timeout.
return result;
}
void loop() {
command("+CLIP",1000);
delay(2000);
}