I have sync the DS18B20 and RTC to Blynk APP, i just want when DS18B20’s reach 50 °C the motor run for 1 minute i really need Help for my project
details :
• Arduino UNO with Nema 17, 3 Sensors DS18B20, and Driver L298N
• Android 8.0.0
• Blynk server
• Blynk Library version 0.6.1
#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(5, 4); // RX, TX
#include <BlynkSimpleSerialBLE.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <OneWire.h>
#include <Stepper.h>
const int stepsPerRevolution = 800;
OneWire ds(2); // on pin 7 (a 4.7K resistor is necessary) make sure you change this from the original pin 10 to an unused pin.
int adr;
float s1;
float s2;
float s3;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
//Dg-xL4X7Bi8pnGnIeE_GShZIwJ3i_FzO
//4rsZaSxJlLy9biW0PQk-znfcbsAGC2Rl
char auth[] = "4rsZaSxJlLy9biW0PQk-znfcbsAGC2Rl"; // Put your Auth Token here. (see Step 3 above)
SoftwareSerial SerialBLE(5, 4); // RX, TX
BlynkTimer timer;
WidgetRTC rtc;
// Digital clock display of the time
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
Serial.print("Current time: ");
Serial.print(currentTime);
Serial.print(" ");
Serial.print(currentDate);
Serial.println();
// Send time to the App
Blynk.virtualWrite(V1, currentTime);
// Send date to the App
Blynk.virtualWrite(V2, currentDate);
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
}
void setup()
{
// set the speed at 60 rpm:
// myStepper.setSpeed(100);
Serial.begin(9600); // See the connection status in Serial Monitor
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Waiting for connections...");
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
// Display digital clock every 10 seconds
timer.setInterval(10000L, clockDisplay);
}
void loop()
{
Blynk.run(); // All the Blynk Magic happens here...
timer.run();
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, -1000, 1000, -1500, -1500);
// set the motor speed:
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
// Serial.print("ROM =");
for( i = 0; i < 8; i++) { //we need to drop 8 bytes of data
}
adr = (addr[7]);
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes to drop off
data[i] = ds.read();
}
Serial.println();
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
if(adr == 229) { //replace ??? with value of sensor number 1
s1 = celsius; //change celsius to fahrenheit if you prefer output in Fahrenheit
}
if(adr == 235) { //replace ??? with value of sensor number 2
s2 = celsius; //change celsius to fahrenheit if you prefer output in Fahrenheit
}
if(adr == 16) { //replace ??? with value of sensor number 3
s3 = celsius; //change celsius to fahrenheit if you prefer output in Fahrenheit
}
if (s1 >= 50 && motorSpeed > 0){
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution /100);;
}
Serial.print(" Sensor 1 = ");
Serial.println(s1);
Serial.print(" Sensor 2 = ");
Serial.println(s2);
Serial.print(" Sensor 3 = ");
Serial.println(s3);
Blynk.virtualWrite(V5, s1);
Blynk.virtualWrite(V6, s2);
Blynk.virtualWrite(V7, s3);
}
Due to your loop, this sketch will never run with blynk.
Create a function and call it with a timer.
Remove delay , and make sure the for next loop never exceed 5 seconds.
1 Like
Thanks for your answer but i am really newbie. can u give me some example ?
like this right ?
[Unformatted code removed by moderator]
please help i need to presention this project in 4 hours again
[Unformatted code removed by moderator]
please edit your sketch with 3 backtiks like this ```
1 Like
I’ve removed the unformatted code from two of your posts.
You’re well aware of how to correctly format code with triple backticks, because you did it in your first post.
Please follow the forum rules in future.
I’d suggest that you get the motor working correctly without Blynk, then read this:
before adding Blynk into the equation.
I’d also recommend not leaving your important project work until the last minute before crying for help.
Pete.
3 Likes
yes the motor coded and the temperature coded run if i not combine that
i make this code like this
#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(5, 4); // RX, TX
#include <BlynkSimpleSerialBLE.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <OneWire.h>
const float tempMax = 30.0;// (watch video)
#include <Stepper.h>
const int stepsPerRevolution = 800;
OneWire ds(2); // on pin 7 (a 4.7K resistor is necessary) make sure you change this from the original pin 10 to an unused pin.
int adr;
float s1;
float s2;
float s3;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
//Dg-xL4X7Bi8pnGnIeE_GShZIwJ3i_FzO
//AtAZzlRjrtJAPKZ-vKdgj1PTVO2NEt5i
//4rsZaSxJlLy9biW0PQk-znfcbsAGC2Rl
char auth[] = "4rsZaSxJlLy9biW0PQk-znfcbsAGC2Rl"; // Put your Auth Token here. (see Step 3 above)
SoftwareSerial SerialBLE(5, 4); // RX, TX
BlynkTimer timer;
WidgetRTC rtc;
// Digital clock display of the time
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
Serial.print("Current time: ");
Serial.print(currentTime);
Serial.print(" ");
Serial.print(currentDate);
Serial.println();
// Send time to the App
Blynk.virtualWrite(V1, currentTime);
// Send date to the App
Blynk.virtualWrite(V2, currentDate);
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
}
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Waiting for connections...");
pinMode(2,OUTPUT);// for EN1
digitalWrite(2,HIGH);// enable EN1
pinMode(3,OUTPUT);// for EN1
digitalWrite(3,HIGH); // enable EN2
Serial.print(" Sensor 1 = ");
Serial.println(s1);
Serial.print(" Sensor 2 = ");
Serial.println(s2);
Serial.print(" Sensor 3 = ");
Serial.println(s3);
Blynk.virtualWrite(V5, s1);
Blynk.virtualWrite(V6, s2);
Blynk.virtualWrite(V7, s3);
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
// Display digital clock every 10 seconds
timer.setInterval(10000L, clockDisplay);
}
void motor()
{
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, -2000, 2000, -1000, 1000);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
//myStepper.step(stepsPerRevolution / 100);
myStepper.step(stepsPerRevolution / 800);
}
}
void suhu()
{
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}
// Serial.print("ROM =");
for( i = 0; i < 8; i++) { //we need to drop 8 bytes of data
}
adr = (addr[7]);
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes to drop off
data[i] = ds.read();
}
Serial.println();
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
if(adr == 229) { //replace ??? with value of sensor number 1
s1 = celsius; //change celsius to fahrenheit if you prefer output in Fahrenheit
}
if(adr == 235) { //replace ??? with value of sensor number 2
s2 = celsius; //change celsius to fahrenheit if you prefer output in Fahrenheit
}
if(adr == 16) { //replace ??? with value of sensor number 3
s3 = celsius; //change celsius to fahrenheit if you prefer output in Fahrenheit
}
}
void loop()
{
Blynk.run(); // All the Blynk Magic happens here...
timer.run();
}
You need a timer to call motor and suhu
Try this :
timer.setInterval(500L, motor );
timer.setInterval(1400L, suhu );
1 Like
the motor now move just take a little step. btw i am sorry, i need the motor run when s1 get 30ºC or s2 get 30ºC or s3 get 30ºC for 30 seconds. thanks for answer me
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Waiting for connections...");
pinMode(2,OUTPUT);// for EN1
digitalWrite(2,HIGH);// enable EN1
pinMode(3,OUTPUT);// for EN1
digitalWrite(3,HIGH); // enable EN2
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
// Display digital clock every 10 seconds
timer.setInterval(10000L, clockDisplay);
timer.setInterval(500L, motor );
timer.setInterval(1400L, suhu );
}
Sorry, this is because the timer code was an example.
I don’t have a stepper motor to do the test.
Please ask @PeteKnight, I know he uses stepper motors.
1 Like
No… you help for suhu function. now i just need code for run if s1 get 30ºC or s2 get 30ºC or s3 get 30ºC for 30 seconds. Thanks
No I gave you ‘motor timer’ too
I think it’s not the good way to doo
waiting for @PeteKnight or @khoih to help you about stepper motor
The stuff I’ve done with stepper motors was a long time ago, and as I’m on holiday I don’t have the code with me at the moment, so I’m not going to be of any help.
You should remove this line though…
Pete.
1 Like
done @PeteKnight i try to improve make function like this but its not work correctly
void TempToMotor()
{
if (s1 >= 30)
{
motor;
}
}
1 Like
Should it not be motor(); ?
1 Like