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.
Related
Need your help again: I'm doing this time Master - Slave Using SPI communication, there is no error in the code when I simulate the code but the LED won't turn on.
The supposed outcome that should happen is that when I push the push button on master board the LED on the slave board will turn on.
Master code:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
digitalWrite(SS, HIGH);
delay(1000);
}
Slave code:
// Slave Board
#include <SPI.h>
#define led1 2
volatile byte Slavereceived;
volatile boolean received;
int x;
void setup(void) {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
received = false;
SPI.attachInterrupt();
}
ISR (SPI_STC_vect) {
Slavereceived = SPDR;
received = true;
}
void loop() {
if (received) {
if (Slavereceived == 1) {
digitalWrite(led1, HIGH);
} else {
digitalWrite(led1, LOW);
}
delay(1000);
}
}
I too was stuck in the same situation, there is no support for the SPI library in tinkercad, you can include it without errors, and even use it, but any useful command will let the code stuck at that command
Sorry, but there no much you can do
this link if for a tinkercad forum, where one of the people said SPI library amoung two others are not supported
Add SPI.transfer(x); below the if else to your master code.
The master code will look somewhat like this:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
SPI.transfer(x);
digitalWrite(SS, HIGH);
delay(1000);
}
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!"));
}
}
Hi I want to connect the arduino pro mini to my AtTiny85 over I2C.
The arduino should tell the attiny to switch a LED on or off.
The arduino manages to switch the led on my attiny on but it never goes off.
I don't have any clue why?
Here is my Code for master and slave:
MASTER:
#include <Wire.h>
#define device (1)
void setup() {
// put your setup code here, to run once:
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(device);
Wire.write(1);
Wire.endTransmission();
delay(2000);
Wire.write(0);
Wire.endTransmission();
delay(2000);
}
SLAVE:
#include <TinyWireS.h>
#include <usiTwiSlave.h>
#define output (4)
#define I2C_SLAVE_ADDR (1)
void setup() {
// put your setup code here, to run once:
TinyWireS.begin(I2C_SLAVE_ADDR);
pinMode(output, OUTPUT);
}
volatile byte msg = 0;
void loop() {
if(TinyWireS.available())
msg = TinyWireS.receive();
if(msg == 1)
digitalWrite(output, HIGH);
else if(msg == 0)
digitalWrite(output, LOW);
else
msg = 0;
}
I finally found my mistake:
when I do: Wire.write(0); I forgot to start the transmission with: Wire.beginTransmission(device);
I have two Arduino Unos, two lcds and Xbee s2.
I connected two xbee s2 module to each arduino uno.
I searched this implementation hard and tried it.
but, it didn't work. I appreciate your help.
Configuration: AP=2
Coordinator API and Router API
Sender: Router
#include <XBee.h>
#include <SoftwareSerial.h
#include <LiquidCrystal.h>
#define LED 13
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial xSerial(3,4);
XBee xbee=XBee();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
void setup(){
//Serial.begin(9600);
xSerial.begin(9600);
xbee.setSerial(xSerial);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, Gang!");
//Serial.println("xbee start");
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
delay(3000);
}
void loop(){
ZBTxRequest zbTx;
uint8_t payload[]={'H', 'E', 'Y', '\0'};
XBeeAddress64 address=XBeeAddress64(0x13a200, 0x40b450f4);
zbTx=ZBTxRequest(address, payload, sizeof(payload));
xbee.send(zbTx);
if (xbee.readPacket(500)) {
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
xbee.getResponse().getZBTxStatusResponse(txStatus);
// get the delivery status, the fifth byte
if (txStatus.getDeliveryStatus() == SUCCESS) {
lcd.setCursor(0,1);
lcd.print("Success");
} else {
// the remote XBee did not receive our packet. is it powered on?
}
}
} else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
} else {
// local XBee did not provide a timely TX Status Response -- should not happen
}
delay(1000);
}
Receiver: Coordinator
#include <SoftwareSerial.h>
#include <XBee.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
SoftwareSerial xSerial(3,4);
XBee xbee=XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
void setup(){
xSerial.begin(9600);
xbee.setSerial(xSerial);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, tang!");
delay(3000);
}
void loop(){
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
lcd.setCursor(0, 1);
lcd.print( "MSG");
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
// got a zb rx packet
// now fill our zb rx class
xbee.getResponse().getZBRxResponse(rx);
lcd.setCursor(0, 1);
lcd.print(rx.getData(1));
if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
// the sender got an ACK
lcd.setCursor(0,1);
lcd.print( "ACK");
} else {
lcd.setCursor(0,1);
lcd.print("ERROR");
}
} else {
// not something we were expecting
//flashLed(errorLed, 1, 25);
lcd.print("7 print");
}
} else if (xbee.getResponse().isError()) {
lcd.print("error");
}
delay(3000);
}
Ah, I connect xbee s2 to xbee pro shield on arduino. There are no dline/uart switch on xbee shield. so, I use softwareserial library...But it didn't work.
I have done the communication between two XBee with Arduino, you can take a look at my coding and try to change your coding a little bit.
Sender =
#include <SoftwareSerial.h>
#define Dout 2
#define Din 3
#define LED 9
SoftwareSerial XBeeSerial (Dout, Din);
void setup() {
XBeeSerial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
delay(500);
XBeeSerial.write("+");
analogWrite(LED,13);
delay(3000);
XBeeSerial.write("-");
analogWrite(LED,0);
delay(500);
}
Receiver =
#include<SoftwareSerial.h>
#define LED 9
#define Dout 2
#define Din 3
SoftwareSerial XBeeSerial( Dout, Din);
char XBee_message;
void setup() {
Serial.begin(9600);
XBeeSerial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
while(XBeeSerial.available()) {
XBee_message = XBeeSerial.read();
Serial.print(XBee_message);
switch (XBee_message){
case '+':
analogWrite(LED, 13);
break;
case '-':
analogWrite(LED, 0);
break;
default:
analogWrite(LED, 0);
}
}
}