I want to convert a string to different output pins.
My output is myString = 01101
Now I want to save
Bit 1 --> Digital Output LED 1 (which is Pin Number 4 on my Controllino Hardware )
Bit 2 --> Digital Output LED 2 (------------------- 5 --------------------------)
Bit 3 --> Digital Output LED 3 (--------------------6---------------------------)
Bit 4 --> Digital Output LED 4 (--------------------7---------------------------)
Bit 5 --> Digital output LED 5 (--------------------8---------------------------)
Now for example I got output myString = 00011 then LED1 and LED2 must go ON and all other should be OFF.
I tried many different way but not getting appropriate result.
Please read the "how to" at the top of each Forum and learn how to properly include your code within an inquiry. You will find that you will get more assistance that way.
This one is bit different from it but related to the last thread.
I am new to this forum and sorry about that.
next time will ask in same thread.
Thank you for the information
Yeah, binary converted to a String, then from String to LEDs on pins. Totally different to this thread.
A rose by any other name.....
(Not to worry, I won't tell anyone if you don't.)
You shouldn't need a String, as you were told in the other thread. Using the String class at all is a bad habit to get into, and definitely not needed for the conversion that you want to do.
BTW The String class works now to the best of my knowledge, the space-leak bugs have been
fixed. It is always going to be more memory intensive that c-strings, but it's much easier to use
for some things - don't dismiss it out of hand based on past bugs, but be aware of the space and
time issues.
MarkT:
BTW The String class works now to the best of my knowledge, the space-leak bugs have been
fixed. It is always going to be more memory intensive that c-strings, but it's much easier to use
for some things - don't dismiss it out of hand based on past bugs, but be aware of the space and
time issues.
Thanks Mark, I wasn't aware that it had been patched up. Personally, I like C strings, so will continue to use them for most things myself, but won't hammer the String class as much from now on.
And for this purpose, where it's needed to take the value originally stored in an int, and set pins according to it's bits, I don't think a String is appropriate.
Bits are numbered from zero for the LSB upwards (unless you work for IBM in the 1970's, in
which case bits are numbered from 1 from the MSB downwards!!)
#define LED1 4 // define your named constants
#define LED2 5
byte pins[] = { LED1, LED2, .... } ; //
// then the business logic like this, the number is in value (which is a byte or int)
for (byte i = 0 ; i < sizeof(pins) ; i++)
digitalWrite (pin[i], (value & (1<<i)) != 0 ? HIGH : LOW) ;
Note the selection of a single bit by &, then using that in a conditional expression (which allows
you to swap LOW and HIGH for active-low outputs easily and makes the intent clearer I think
OldSteve:
Thanks Mark, I wasn't aware that it had been patched up. Personally, I like C strings, so will continue to use them for most things myself, but won't hammer the String class as much from now on.
Agree, and I feel the same way, probably because I started using C in the late 70's. Still, I think I'll continue to hammer the String class because it seems to fritter away a lot of resources. I'm kinda the same way with very robust functions where you only use a very small part of their functionality because there are simpler ways to do the same task (e.g., sprintf()). Still, it's good to know the fragmentation issues are fixed.
Delta_G:
How long is that code? Could it be posted in code tags so those of us on mobile devices can see it? That will likely get you a lot more help than attaching it.
Quicker if I do it. I formatted it and removed all of the superfluous empty lines while I was at it, but otherwise changed nothing:-
#define FPS_to_KMH 1.09728 // scale factor
#define DISTANCE 7 // distance between the two sensors in feet.
#define LED1 4 // define your named constants
#define LED2 5
#define LED3 6
#define LED4 7
#define LED5 8
int SensorOnePin = A0;
int SensorTwoPin = A1;
int Byte1 = 4;
byte SensorOneState;
byte SensorTwoState;
float Speed = 0, KMH = 0, tmp = 0;
boolean GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;
void setup()
{
Serial.begin(9600);
pinMode(SensorOnePin, INPUT);
pinMode(SensorTwoPin, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop()
{
SensorOneState = digitalRead(SensorOnePin);
SensorTwoState = digitalRead(SensorTwoPin);
if (SensorOneState && GotSecondSensor == false) // Train drives through first "sensor"
{
SensorOne_timer = millis(); // record time
GotSecondSensor = true; // lockout this IF statement and unlock the next IF statement
}
if (SensorTwoState && GotSecondSensor == true)
{
SensorTwo_timer = millis(); //record the time the train reaches the second gate
Speed = GetSpeed(SensorOne_timer, SensorTwo_timer, DISTANCE); // send the times and the distance into the function.
Serial.print("KMH: ");
Serial.println(Speed);
GotSecondSensor = false; // unlock first IF statement and lockout this IF statement.
}
}
float GetSpeed(unsigned long T1, unsigned long T2, float distance)
{
KMH = distance * (FPS_to_KMH); // "(FPS_to_MPH)" -> conversion factor, feet per second to miles per hour
tmp = (T2 - T1) / 1000.00; // since the time we are using is in milliseconds, we need to convert milliseconds to seconds
Serial.print("Time (seconds): ");
Serial.println(tmp);
int myNum = KMH / tmp;
int zeros = String(myNum, BIN).length();
String myStr;
for (int i = 0; i < zeros; i++)
myStr = myStr + "0";
myStr = myStr + String(myNum, BIN);
Serial.print("Binar Codiert :");
Serial.println(myStr);
byte pins[5] = { LED1, LED2, LED3, LED4, LED5} ;
for (byte i = 0 ; i < sizeof(pins) ; i++)
digitalWrite (pins[i], ( 16 & (1 << i)) != 0 ? HIGH : LOW) ;
return (myNum);
}
It's 1am, and I'm too tired to do battle with it myself.
Delta_G:
How long is that code? Could it be posted in code tags so those of us on mobile devices can see it? That will likely get you a lot more help than attaching it.
Quite true, Read how to post source code using code tags in Nick Gammon's How To post at the top of this Forum. Readers here are often shy about uploading and opening files they are not sure about, which has the effect of reducing the number of people who read your code.
econjack:
Quite true, Read how to post source code using code tags in Nick Gammon's How To post at the top of this Forum. Readers here are often shy about uploading and opening files they are not sure about, which has the effect of reducing the number of people who read your code.
econjack, I just did it for him, posted in the reply before your's.
(Now I'm off to bed.)
Youre pretty close, but you lost the variable you want to check
And forget about the display beauty of a binary representation with leading zeros for the moment.
Even for that task you're better off without String objects, IMO.
Please give me suggestions so I can modify my programm to measure speed in both direction.
I mean if I consider this whole programm for "Forward" direction.
Can I make it for "Backward" in same logic?
#define FPS_to_KMH 1.09728 // scale factor
#define DISTANCE 7 // distance between the two sensors in feet.
#define LED1 4 // define your named constants
#define LED2 5
#define LED3 6
#define LED4 7
#define LED5 8
int SensorOnePin = A0;
int SensorTwoPin = A1;
int Byte1 = 4;
byte SensorOneState;
byte SensorTwoState;
float Speed = 0, KMH = 0, tmp = 0;
boolean GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;
void setup()
{
Serial.begin(9600);
pinMode(SensorOnePin, INPUT);
pinMode(SensorTwoPin, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop()
{
SensorOneState = digitalRead(SensorOnePin);
SensorTwoState = digitalRead(SensorTwoPin);
if(SensorOneState && GotSecondSensor == false) // Train drives through first "sensor"
{
SensorOne_timer = millis(); // record time
GotSecondSensor = true; // lockout this IF statement and unlock the next IF statement
}
if(SensorTwoState && GotSecondSensor == true)
{
SensorTwo_timer = millis(); //record the time the train reaches the second gate
Speed = GetSpeed(SensorOne_timer, SensorTwo_timer, DISTANCE); // send the times and the distance into the function.
Serial.print("KMH: ");
Serial.println(Speed);
GotSecondSensor = false; // unlock first IF statement and lockout this IF statement.
}
}
int GetSpeed(unsigned long T1, unsigned long T2, float distance)
{
KMH = distance * (FPS_to_KMH); // "(FPS_to_MPH)" -> conversion factor, feet per second to miles per hour
tmp = (T2 - T1)/1000.00; // since the time we are using is in milliseconds, we need to convert milliseconds to seconds
Serial.print("Time (seconds): ");
Serial.println(tmp);
Serial.println ("Forward");
int myNum = KMH/tmp;
byte pins[5] = { LED1, LED2, LED3, LED4, LED5} ;
for (byte i = 0 ; i < sizeof(pins) ; i++)
digitalWrite (pins[i], ( myNum & (1<<i)) != 0 ? HIGH : LOW) ;
Serial.print("Binar Codiert :");
Serial.println(myNum, BIN);
return (myNum);
}
You wrote the code to get it working in the forward direction, so with a little time and thought you should be able to do the same for the opposite direction.
It's largely just a matter of swapping the order of these:-
if(SensorOneState && GotSecondSensor == false) // Train drives through first "sensor"
.
.
.
if(SensorTwoState && GotSecondSensor == true)
Perhaps set/clear a flag to indicate direction, then do the above in either of two ways based on the state of the flag.
You might need to do a little more, but that will help get you started.
It's getting too late at night for me to look (or think) further, but it will be good practice if you figure out the rest for yourself.