Hey guys, new to the world of arduino, had fun so far but stuck here. Not so good with c++, I have a project that for short: I receive 4 digit code lets say (0000) and need to get the first two digits (00), (I do that with "string" command) and use rotary encoder to dial a new numbers but in this order: 01, 02, 03, 04, 05, 06, 07 and when reach 07 I need it to jumb to the 10s -> 10, 11, 12... and so on. So basically I need a counter that reaches up to 7 and start over. Also on the serial print I need "0" in front of the first combination (just like the example) not just "1" but 01, 02...... Hope that make sense, will be glad to get a little "push" with this one.
Sounds a lot like Octal. Do you have anything that works? Code?
Look at
and see if this helps:
x = 7;
if(x < 8) Serial.print('0');
Serial.print(x,OCT);
I don't have anything yet. Still cant figer out how to start. The only code I have is connector that recieves the 4-digit code and a loop to debounce and smooth rotray encoder which gives me the increase and decrease int which I want to use in this counter.
Show us what do you have.
its not related to this function
Expand on that, please.
const int TCASleftDT=5;
const int TCASleftCLK=6;
long TimeOfLasDebounce1 = 0;
int DelayofDebounce1 = 0.01;
int PreviousTCASleftCLK;
int PreviousTCASleftDATA;
int SQ_left_inc, SQ_left_dec = 0;
void setup() {
Serial.begin(115200);
PreviousTCASleftCLK=digitalRead(TCASleftCLK);
PreviousTCASleftDATA=digitalRead(TCASleftDT);
}
void check_rotary_tcas_left() {
if ((PreviousTCASleftCLK == 0) && (PreviousTCASleftDATA == 1)) {
if ((digitalRead(TCASleftCLK) == 1) && (digitalRead(TCASleftDT) == 0)) {
Serial.println(SQ_left_inc);
}
if ((digitalRead(TCASleftCLK) == 1) && (digitalRead(TCASleftDT) == 1)) {
Serial.println(SQ_left_dec);
}
}
if ((PreviousTCASleftCLK == 1) && (PreviousTCASleftDATA == 0)) {
if ((digitalRead(TCASleftCLK) == 0) && (digitalRead(TCASleftDT) == 1)) {
Serial.println(SQ_left_inc);
}
if ((digitalRead(TCASleftCLK) == 0) && (digitalRead(TCASleftDT) == 0)){
Serial.println(SQ_left_dec);
}
}
if ((PreviousTCASleftCLK == 1) && (PreviousTCASleftDATA ==1)) {
if ((digitalRead(TCASleftCLK) == 0) && (digitalRead(TCASleftDT) == 1)) {
Serial.println(SQ_left_inc);
}
if ((digitalRead(TCASleftCLK) == 0) && (digitalRead(TCASleftDT) == 0)){
Serial.println(SQ_left_dec);
}
}
if ((PreviousTCASleftCLK == 0) && (PreviousTCASleftDATA == 0)) {
if ((digitalRead(TCASleftCLK) == 1) && (digitalRead(TCASleftDT) == 0)) {
Serial.println(SQ_left_inc);
}
if ((digitalRead(TCASleftCLK) == 0) && (digitalRead(TCASleftDT) == 1)){
Serial.println(SQ_left_dec);
}
}
}
void loop() {
// Rotary Transponder Left debounce check:
if ((millis() - TimeOfLasDebounce1) > DelayofDebounce1){
check_rotary_tcas_left();
PreviousTCASleftCLK=digitalRead(TCASleftCLK);
PreviousTCASleftDATA=digitalRead(TCASleftDT);
TimeOfLasDebounce1=millis();
}
}
String stringMHz = Com1ActiveFreq.substring(0,3); ( just example in my other code)
No, sorry, that looks more like a contraction.
Good luck with the rest of it.
hmm
prints the leading zero
prints the value of x in octal digits
binary digits: 0,1
octal digits: 0,1,2,3,4,5,6,7
decimal digits: 0,1,2,3,4,5,6,7,8,9
hexadecimal digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
now it depends on what you want to do with these digits
for simply printing them this line of code does what you described
Serial.print(x,OCT);
If I would reduce information to just your question I could write
use the NewEncoder-library for counting up/down a variable
Though some more info is important.
NewEncoder requires interruptcapable-IO-pins
if you can connect the encoder to such IO-pins depends on the overall used hardware of your project
If you want to do different things than just printing the digits you might can use the SafeString-library which has the print-command too.
best regards Stefan
depending on your
Thanks for the direction. I will take a look at OCT. Basicaly what I need is:
1.Recieve that 4 digit code (0000) by serail com - I already got that in my code
2. make it 2 parts - 1st two digits and last two - 00 00:
String stringPart1 = recievedCode.substring(0,2);
String stringPart2 = recievedCode.substring(2,4);
I do that because I will have 2 rotary encoders - one for the first 2 digits and the second for the last two digits.
3.Use my encoder to modify stringPart1, and for every turn I check and compare new code and old code so I send command to update it.
Let's say originaly its "47" - I rotate one up and the new code is "50" compare that to "oldstringPart1" and if it's changed it must send command to update the source via serial com.
Why the obsession with String representation?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.