how to power on the pump on arduino - if-statement

I want to make a project that can start the pump according to the following conditions
the pump 1 will on if "jaraak" less than 5
the pump 2 will on if "jaraak" more than 10
the pump 3 and 4 wiil on if "tdsvalue" less than 700
the pump 2 will on if "tdsvalue" more than 800
Is the code that I made correct, because in my code just pump 1 always on
if (jaraak <= 5)
{ antares.getInt("jaraak");
antares.add("Tandon Aman, jarak air " , jaraak );
digitalWrite(pompa1, pompaON);
digitalWrite(pompa2, pompaOFF);
}
if (jaraak >= 10)
{ antares.getInt("jaraak");
antares.add("Tandon Habis, jarak air " , jaraak );
digitalWrite(pompa1, pompaOFF);
digitalWrite(pompa2, pompaON);
}
if (tdsValue < 700)
{ antares.getFloat("tdsValue");
antares.add("\n Kepekatan Nutrisi (ppm)" , tdsValue );
{digitalWrite(pompa3, pompaON);
digitalWrite(pompa4, pompaON);}
}
if (tdsValue > 800)
{ antares.getFloat("tdsValue");
antares.add("\n Kepekatan Nutrisi (ppm)" , tdsValue );
digitalWrite(pompa3, pompaOFF);
digitalWrite(pompa4, pompaOFF);
digitalWrite(pompa2, pompaON);
}

The code doesn't consider all the possible values of the variables. Consider this alternative:
if (jaraak < 5) // You say: "the pump 1 will on if jaraak LESS than 5"
{ // "the pump 2 will on if jaraak MORE than 10"
// ...
digitalWrite(pompa1, pompaON);
digitalWrite(pompa2, pompaOFF);
}
else if (jaraak <= 10) // <-- This is the case when 5 <= jaraak <= 10
{
// ...
digitalWrite(pompa1, pompaOFF);
digitalWrite(pompa2, pompaOFF);
}
else // <-- This is the case when jaraak > 10
{
// ...
digitalWrite(pompa1, pompaOFF);
digitalWrite(pompa2, pompaON);
}
// The same for tdsvalue

Related

Arduino millis() print values one after other

I don't know if someone else asked this before.
I'm trying to print in Serial screen the number 1 - 6, ONE number every 5 seconds (I'm trying only with number 1 and number 2 for smaller and easier to undestand and modify code). I could use delay function, but i want to use millis function, because i don't want to block the code using delay in loop.
This code problem is a part of bigger project.
I tryied to print the numbers using different interval times (eventTime_1 and eventTime_2), but it didn't work.
The code is
/* Two independant timed evenets */
const unsigned long eventTime_1 = 1000; // interval in ms
const unsigned long eventTime_2 = 5000;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
void setup() {
// To Do: Serial communication
Serial.begin(9600);
}
void loop() {
/* Updates frequently */
unsigned long currentTime = millis();
// To Do: Event 1 timing
/* This is event 1 stuff */
if ( currentTime - previousTime_1 >= eventTime_1 ) {
Serial.println (" 1 ");
// Serial.println(currentTime);
// Serial.println(previousTime_1);
/* Update the timing for the next event */
previousTime_1 = currentTime;
}
// To Do: Event 2 timing
/* This is event 2 stuff */
if (currentTime - previousTime_2 >= eventTime_2 ) {
Serial.println ("2");
// Serial.println( analogRead(tempSensor) );
/* Update the timing for the next event */
previousTime_2 = currentTime;
}
}
As a result, prints 5 times the number 1 and after 5 seconds 1 time the number 2.
This is what i ecpect:
12:16:53.212 -> 1
12:16:58.225 -> 2
12:17:03.233 -> 1
12:17:08.238 -> 2
12:17:13.203 -> 1
12:17:18.272 -> 2
This is the final working code. From this code you can print 1 and 2 alternating every X second (x depends from you, in eventTime = X ; In my code is 5000).
// To Do: Variables for Timed Events
/* Create timed evenets */
const unsigned long eventTime = 5000;
unsigned long previousTime = 0;
/* Create a flag */
boolean flag1 = false;
void setup() {
// To Do: Serial communication
Serial.begin(9600);
}
void loop() {
/* Updates frequently */
unsigned long currentTime = millis();
if (currentTime - previousTime >= eventTime ) {
if (flag1 == false) {
Serial.println("1");
flag1 = true;
}
else if (flag1 == true) {
Serial.println ("2");
flag1 = false;
}
/* Update the timing for the next event */
previousTime = currentTime;
}
}
And the results are:
15:32:46.342 -> 1
15:32:51.402 -> 2
15:32:56.327 -> 1
15:33:01.325 -> 2
15:33:06.328 -> 1
15:33:11.325 -> 2
15:33:16.327 -> 1

Omnet++ A cRuntimeError exception is about to be thrown std::length_error: basic_string::_M_create

so I get this error when i run my code
Error in module (TraCIDemoRSU11p) RSUExampleScenario.rsu[0].appl (id=8) at event #4000, t=45.40151998544: std::length_error: basic_string::_M_create.
TRAPPING on the exception above, due to a debug-on-errors=true configuration option. Is your debugger ready?
it shows that I have this issue at this line after looping multiple times
VehTD = *iteh2;
please be informed that I'm trying to remove from this vehicle list to another vehicle list and delete the moved items.
std::list<std::pair<std::string,std::string>>::const_iterator iteh2 = waitingList.begin();
for (std::list<std::pair<std::string,std::string>>::const_iterator iteh = waitingList.begin(); iteh != waitingList.end() && !waitingList.empty(); ){
iteh2 = iteh;
cout<<"Veh ID TF 1:";
cout<<VehTD.first<<endl;
cout<<VehTD.second<<endl;
VehTD = *iteh2;
cout<<"Veh ID TF 2:";
cout<<VehTD.first<<endl;
cout<<VehTD.second<<endl;
CompareResult3 = Locks(Locked,VehTD.second);
if(CompareResult3.second == 1 || CompareResult3.second == 2 ){
//remove from waiting and add to crossing
std::string ehk = "";
simtime_t VehicleT = simTime();
std::pair<std::pair<std::string, std::string>, simtime_t> VehicleWithTime;
crossingList.push_back(VehTD);
iteh2 = waitingList.erase(iteh2);
}
else{
++iteh;
}
}
}
I'm using Oment++ 5.0 and veins 4.4
I put a try and catch at this function, and it seems that the exception happens towards the end of the list.
Update:
I have this output:
WCounter 1
WCounter 2
Moving from waiting list Timer Function Exception
WCounter 1
WCounter 2
WCounter 3
Moving from waiting list Timer Function Exception
WCounter 1
WCounter 2
Moving from waiting list Timer Function Exception
The WCounter is a variable to show how many times it looped before having the exception at the point explained earlier.
based on request I added the locks function:
std::pair<std::list<std::string>,int> TraCIDemoRSU11p::Locks(std::list<std::string> alreadyLocked, std::string laneNo){
bool debugL;
//create variables for the lanes for easier use
try{
debugL = false;
zero = "171270266#0_0";
one = "171270266#0_1";
two = "-171270025#1_0";
three = "-171270025#1_1";
four = "-171270266#1_0";
five = "-171270266#1_1";
six = "171270025#0_0";
seven = "171270025#0_1";
//create lists of locks
Locks0 = Locks1 = Locks2 = Locks3 = Locks4 = Locks5 = Locks6 = Locks7 = RequestToLock = {};
CounterOfSimilarLanes = 0;
//set of 0 ,Locks0;
Locks0.push_back(zero);
Locks0.push_back(five);
Locks0.push_back(seven);
//set of 1 ,Locks1;
Locks1.push_back(one);
Locks1.push_back(three);
Locks1.push_back(six);
//set of 2 ,Locks2;
Locks2.push_back(one);
Locks2.push_back(two);
Locks2.push_back(seven);
//set of 3 ,Locks3;
Locks3.push_back(zero);
Locks3.push_back(three);
Locks3.push_back(five);
//set of 4 ,Locks4;
Locks4.push_back(one);
Locks4.push_back(three);
Locks4.push_back(four);
//set of 5 ,Locks5;
Locks5.push_back(two);
Locks5.push_back(five);
Locks5.push_back(seven);
//set of 6 ,Locks6;
Locks6.push_back(three);
Locks6.push_back(five);
Locks6.push_back(six);
//set of 7 ,Locks7;
Locks7.push_back(one);
Locks7.push_back(four);
Locks7.push_back(seven);
//This is the request to lock from the vehicle using its lane number
if (laneNo == zero ){
RequestToLock.insert(RequestToLock.end(),Locks0.begin(),Locks0.end());
}else if (laneNo == one){
RequestToLock.insert(RequestToLock.end(),Locks1.begin(),Locks1.end()) ;
}else if (laneNo == two){
RequestToLock.insert(RequestToLock.end(),Locks2.begin(),Locks2.end()) ;
}else if (laneNo == three){
RequestToLock.insert(RequestToLock.end(),Locks3.begin(),Locks3.end()) ;
}else if (laneNo == four){
RequestToLock.insert(RequestToLock.end(),Locks4.begin(),Locks4.end()) ;
}else if (laneNo == five){
RequestToLock.insert(RequestToLock.end(),Locks5.begin(),Locks5.end()) ;
}else if (laneNo == six){
RequestToLock.insert(RequestToLock.end(),Locks6.begin(),Locks6.end()) ;
}else if (laneNo == seven){
RequestToLock.insert(RequestToLock.end(),Locks7.begin(),Locks7.end()) ;
}
} //if the already locked from the controller is empty so we take the requested lock
catch(const std::exception &e){
cout<<"Preparing Locks Algorithm Exception"<<endl;
}
try{
if (alreadyLocked.empty()){
markOfLocks = 0;
alreadyLocked.insert(alreadyLocked.end(),RequestToLock.begin(),RequestToLock.end()) ;
Locked.insert(Locked.end(),RequestToLock.begin(),RequestToLock.end()) ;
if (debugL == true){
cout<<"First Lock in the locking algorithm"<<endl;
// dumplistLock(Locked);
}
ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
}else{ // if the already locked by the controller isnt empty, so we need to compare the request with it
//Search for similar items in the 2 lists
for(std::list<std::string>::/*const_*/iterator it_1 = alreadyLocked.begin();it_1 != alreadyLocked.end();)
{
for(std::list<std::string>::/*const_*/iterator it_2 = RequestToLock.begin(); it_2 != RequestToLock.end();)
{
if(*it_1 == *it_2)
{
++CounterOfSimilarLanes;
}
++it_2;
}
++it_1;
}
if (RequestToLock == alreadyLocked){
markOfLocks = 1;
Locked.clear();
Locked = {};
Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
if (debugL == true){
cout<<"Same Lane"<<endl;
dumplistLock(Locked);
}
ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
}else if(CounterOfSimilarLanes == 0){ //if the request is concurrent meaning totally different locks
markOfLocks = 2;
//alreadyLocked.insert(alreadyLocked.end(),RequestToLock.begin(),RequestToLock.end()) ;
Locked.clear();
Locked = {};
Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
if (debugL == true){
cout<<"Concurrent Lane"<<endl;
dumplistLock(Locked);
}
}else if(CounterOfSimilarLanes == 1 || CounterOfSimilarLanes == 2){ // if there's a similarity is 1 lock at least so it leads to conflict
markOfLocks = 3;
Locked.clear();
Locked = {};
Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
if (debugL == true){
cout<< "opposite Lane"<<endl;
dumplistLock(Locked);
}
ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
}else{
markOfLocks = 1;
Locked.clear();
Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
if (debugL == true){
cout<<"default case"<<endl;
dumplistLock(Locked);
}
ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
}
}
}catch(const std::exception &e){
cout<<"Lock Cases Exceptions"<<endl;
}
try{
return ReturnOfLocks;
}catch(const std::exception &e){
cout<<"No Return of Locks in Locks Alg Exception"<<endl;
}
}
so I changed the for loop to a while loop and it didn't show an exception, and not sure why.
therefore the loop looks like this now:
//while loop instead.
std::list<std::pair<std::string,std::string>>::iterator iterwil = waitingList.begin();
std::pair<std::string, std::string> Vehwil ;
while (iterwil != waitingList.end()){
Vehwil = *iterwil;
CompareResult3 = Locks(Locked,Vehwil.second);
if(CompareResult3.second == 1 || CompareResult3.second == 2 ){
crossingList.push_back(Vehwil);
waitingList.erase(iterwil++);
}else{
++iterwil;
}
}
if someone has a valid reason to enlighten me on why the while loop works with no exception unlike the for loop that would be great. ^^"
You should use iterator instead of const_iterator because you modify (i.e. delete) indicated element.
By the way, in the code iteh2 is unnecessary - iteh is sufficient as well as VehicleT and ehk variables are not used and may be removed.

Some of these codes don't run [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i'm a beginner at C++ and i tried to make a script for a Trinity Core World of Warcraft private server but it seems that some of the codes ran successfully and the others are escaped for unknown reason..
here's the code
#include "ScriptPCH.h"
#include "DisableMgr.h"
class LearnSpellsOnLevelUp : public PlayerScript
{
public:
LearnSpellsOnLevelUp()
: PlayerScript("LearnSpellsOnLevelUp"){};
void OnLevelChanged(Player* player, uint8 oldLevel)
{
if (player->getClass() == 3 && player->getLevel() >= 12) // ran
{
if (player->HasSpell(23356)) // ran
return; // ran
else // ran
player->LearnSpell(23356, false); // ran
player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Taming Lesson]!|r"); // ran
}
if (player->getClass() == 9) // Warlock // ran
{
if (player->getLevel() >= 2) // ran
{
if (player->HasSpell(44163)) // ran
return;
else
player->LearnSpell(44163, false); // ran
player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Imp]!|r"); // ran
}
else if (player->getLevel() >= 10)
{
if (player->HasSpell(25112))
return;
else
player->LearnSpell(25112, false);
player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Voidwalker]!|r");
}
else if (player->getLevel() >= 20)
{
if (player->HasSpell(712))
return;
else
player->LearnSpell(712, false);
player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Succubus]!|r");
}
else if (player->getLevel() >= 30)
{
if (player->HasSpell(691))
return;
else
player->LearnSpell(691, false);
player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Felhunter]!|r");
}
else
return;
}
}
};
void AddSC_LearnSpellsOnLevelUp()
{
new LearnSpellsOnLevelUp();
}
i wrote next to the codes that ran successfully // ran
Pay close attention to the order of your getLevel conditions.
if (player->getLevel() >= 2)
{
}
// Here, it's certain that the level is < 2, since it wasn't >= 2.
// Thus, none of these following tests will be true.
else if (player->getLevel() >= 10)
{
}
else if (player->getLevel() >= 20)
{
}
else if (player->getLevel() >= 30)
{
}
else
return;
You should test the levels starting with the greatest one and work your way donwnward.
if (player->getLevel() >= 30)
{
}
// The level is less than 30. Is it greater than 20?
else if (player->getLevel() >= 20)
{
}
// The level is less than 20. Is it greater than 10?
else if (player->getLevel() >= 10)
{
}
// The level is less than 10. Is it greater than 2?
else if (player->getLevel() >= 2)
{
}
else
return;
Looks like there is simply a logic bug. Let's look at this statement:
if (player->getLevel() >= 2) // ran - ok so far
And now a lot of this:
else if (player->getLevel() >= 10) - and others (comparing with 20, 30)
Now suppose your 'player' has 15 level - looks like the intent was to run the second if block (e.g. where you compare with >= 10)
But there is a problem: if the level is 15 then everytime only the first if-block is executed (because obviously 15 >= 2).
Solution: compare to range, like this:
if ((player->getLevel() >= 2) && (player->getLevel() < 10))
...
else if ((player->getLevel() >= 10) && (player->getLevel() < 20))
...
And so on.

Bukkit countdown message sends message 4 times

I just created a countdown method in Java, but i have a problem when the countdowner broadcasts the messages: 60(and down) seconds until the game starts!
The broadcast gets sent *4. Does anyone know any solution to this?
Here is my code:
Main plugin;
public StartCountdown(Main pl) {
plugin = pl;
}
public static int timeUntilStart;
#Override
public void run() {
for(Player p1 : Bukkit.getOnlinePlayers()){
if(timeUntilStart == 0) {
if(!Game.canStart()) {
plugin.restartCountdown();
ChatUtilities.broadcast(ChatColor.RED + "Not enough players to start. Countdown will");
ChatUtilities.broadcast(ChatColor.RED + "restart.");
p1.playSound(p1.getLocation(), Sound.ENDERDRAGON_WINGS, 5, 1);
return;
}
Game.start();
}
for(Player p : Bukkit.getOnlinePlayers()){
p.setLevel(timeUntilStart);
if(timeUntilStart < 11 || timeUntilStart == 60 || timeUntilStart == 30) {
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 0);
if(timeUntilStart == 1) {
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 1);
}
ChatUtilities.broadcast(String.valueOf(timeUntilStart)
+ " §6Seconds until the game starts!");
}
}
}
timeUntilStart -= 1;
}
}
You are broadcasting for every player that is online. You need to move any code that you don't want to run for every player outside of the for loop.
#Override
public void run() {
if (timeUntilStart == 0) {
if (!Game.canStart()) {
plugin.restartCountdown();
ChatUtilities.broadcast(ChatColor.RED + "Not enough players to start. Countdown will");
ChatUtilities.broadcast(ChatColor.RED + "restart.");
for (Player p : Bukkit.getOnlinePlayers()) p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 5, 1);
return;
}
Game.start();
}
boolean broadcast;
for (Player p : Bukkit.getOnlinePlayers()) {
p.setLevel(timeUntilStart);
if (timeUntilStart < 11 || timeUntilStart == 60 || timeUntilStart == 30) {
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 0);
if (timeUntilStart == 1) p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 1);
broadcast = true;
}
}
if (broadcast) ChatUtilities.broadcast(String.valueOf(timeUntilStart) + " §6Seconds until the game starts!");
timeUntilStart -= 1;
}
as Tanner Little said above
You are broadcasting for every player that is online. You need to move any code that you don't want to run for every player outside of the for loop.
You need to make sure as well that you are cancelling the task. I would recommend using the inbuilt scheduler. You can access the scheduler in this way
private int countDownTimer
private int countDownTime
public void runCountDown() {
countDownTimer = Bukkit.getScheduler.scheduleSyncDelayedTask(plugin, new runnable() {
public void run {
if (countDownTime <= 0) {
//do your bradcasting here
for (Player ingame : Bukkit.getOnlinePlayers()) {
//Do your player specific stuff here
}
Bukkit.getScheduler.cancelTask(countDownTimer);
}
if (countDownTime % 10 == 0) { //You can pick whaterver times u want this is just for an example
//Do periodic broadcasting
}
countDownTime -= 1;
}
}, 0L, 20L); //This means that it would wait 0 ticks to start the countdown and do the task every 20 ticks ie) 1 second.
}
Hopefully this helps you.

c++ copy array to array

I have taken code from here Webduino Network Setp
I added one more field.
struct config_t
{
....
...
.....
byte subnet[4];
byte dns_server[4];
unsigned int webserverPort;
char HostName[10]; // Added code Here..
} eeprom_config;
Snippet..
#define NAMELEN 5
#define VALUELEN 10
void setupNetHTML(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
URLPARAM_RESULT rc;
char name[NAMELEN];
char value[VALUELEN];
boolean params_present = false;
byte param_number = 0;
char buffer [13];
.....
.....
}
Added Lines to read date from web page and Wire to eeprom
Write to eeprom: ( Facing issue here, I need to copy value to eeprom_config.HostName[0] ... )
// read Host Name
if (param_number >= 25 && param_number <= 35) {
// eeprom_config.HostName[param_number - 25] = strtol(value, NULL, 10);
eeprom_config.HostName[param_number - 25] = value ; // Facing Issue here..
}
and...
for (int a = 0; a < 10; a++) {
server.printP(Form_input_text_start);
server.print(a + 25);
server.printP(Form_input_value);
server.print(eeprom_config.HostName[a]);
server.printP(Form_input_size1);
server.printP(Form_input_end);
}
Issue was resolved.
Thanks , got idea from this post.
invalid conversion from char' tochar*'
How ! changed
// read Host Name
if (param_number >= 25 && param_number <= 35) {
// eeprom_config.HostName[param_number - 25] = strtol(value, NULL, 10);
eeprom_config.HostName[param_number - 25] = value ; // Facing Issue here..
}
changed to
// read Host Name
if (param_number >= 25 && param_number <= 35) {
eeprom_config.HostName[param_number - 25] = value[0];
}