arrrrr..... my example got trashed when the thread got merged....
Oh dear how sad...
A bug methinks but nothing that can be fixed anymore.
Check your drafts folder maybe you can pick something out of there.
Maybe this will be an easier approach? Or could just make a bigger mess..
void loop(){
byte leds = B10000000;
shiftByteMSF(leds);
delay(500);
byte led = B00000000;
shiftByteMSF(led);
delay(500);
Lets see what you want to do.
shiftByteMSF(B10000000);
wait 1/2 sec
shiftByteMSF(B00000000);
wait 1/2 sec.
over and over.
#include <timeObj.h>
timeObj delayTimer(500,false); // Microwave timer? Twist it and wait 'till it goes "ding()".
bool first; // boolen to tell what part of loop we want to work on.
void setup(void) {
// Do setup things ending in..
shiftByteMSF(B10000000); // Initial call to shift
delayTimer.start(); // Twist the timer.
first = false; // false means the next time through we do second group.
}
loop(void) {
// Do whatever loop things..
if (first && delayTimer.ding()) { // If first is true and the timer went ding..
shiftByteMSF(B10000000); // First call to shift
delayTimer.start(); // Twist the timer..
first = false; // Next time through we want the second section.
} else if (delayTimer.ding()) { // First is false, second section.
shiftByteMSF(B00000000); // Second call to shift
delayTimer.start(); // Twist the timer..
first = true; // Next time through we want the first section.
}
}
I don't know if this will even compile. But. from what I can see. the logic followes what you wrote in your first post. This version is without using delay. timeObj comes from LC_baseTools in your friendly library manager.
-jim lee
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // resets timer every interval
count++; // do something here, increment each time this if() is true
}
if (count == 1 ) {
leds = 1 ; // changes after inteval
}
if (count == 2) {
leds = 0 ; // changes and remains until count = 1
count = 0 ;
this does escencially the same. if the count is 0, do one thing
if the count is 1 to the other.
replace leds with shiftByteMSF(B10000000);
the OP has to add in the flag to allow it to only change state once each time count increments.
we can't do all of his homework for him.
you can make it 1 second and half second.
change interval to 0.1, then the first if() to 10 and the second to 15
first of all sorry for merging the post it happened automatically i just wanted to make a new post. thank you for all the responses but im having difficulties on how to implement your suggestion in my code so i analysed the BlikWithoutDelay code and the bit i was most interested in was this:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
so i was thinking instead of having two binaries that switch led on and off if i some how merge them in to one variable (like ledstate) so when i do use the variable it will basically toggle the led only thing is i dont know how to merge them anyone have any ideas on how to do this?
What "two binaries" are you referring to?
I think it is meant to be "booleans", rather than two different object codes.
aarg:
What "two binaries" are you referring to?
#define DATA 8
#define LATCH A3
#define CLOCK 12
#define LATCH_ON PORTC = PORTC | B00001000
#define LATCH_OFF PORTC = PORTC & B11110111
#define CLOCK_ON PORTB = PORTB | B00010000
#define CLOCK_OFF PORTB = PORTB & B11101111
#define DATA_ON PORTB = PORTB | B00000001
#define DATA_OFF PORTB = PORTB & B11111110
const long interval = 1000;
unsigned long previousMillis = 0;
void shiftByteMSF(byte HB){
LATCH_OFF; //sets latch low before data sending
for(int shiftBit=0; shiftBit<8; shiftBit++) { //repeat 8 times, incrementing .
if(bitRead(HB, 7-shiftBit) == 1) { //read the bit of the variable for sReg, and start from Right-most bit
DATA_ON;
//Serial.println(bitRead(sRegState, 7-shiftBit));
}
else{
DATA_OFF;
//Serial.println(bitRead(sRegState, 7-shiftBit));
}
CLOCK_ON; //cycle clock once data value stable
CLOCK_OFF;
DATA_OFF; //0 on data, if it was already high, after clock pulse
}
LATCH_ON; //latch to output data to register
}
void setup(){
DDRC|=LATCH_ON;
DDRB|=DATA_ON;
DDRB|=CLOCK_ON;
}
void loop(){
byte HB_ON = B10000000;
shiftByteMSF(HB_ON);
if (millis() - previousMillis >= interval) {
// save the last time you blinked the HB_OFF
previousMillis = millis();
byte HB_OFF = B00000000;
shiftByteMSF(HB_OFF);
delay(500);
}
}
so im referring to the binaries for HB_ON and HB_OFF so B1000000 turns the led on and B00000000 turn it off
dave-in-nj:
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // resets timer every interval
count++; // do something here, increment each time this if() is true
}
if (count == 1 ) {
ledState = HIGH ; // changes after interval
}
if (count == 2) {
ledState = LOW ; // changes and remains until count = 1
count = 0 ;
digitalWrite(ledPIN, ledState);
or, you can try :
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // resets timer every interval
ledState= !ledSatate; // set ledState to inverse of it's current state
}
digitalWrite(ledPIN,ledState);
but, as noted before, you have to declare ledState as a global to use the inverse.
so how would i declare ledPIN and ledState given how i have layed out my code as i have 2 variables for each variable i.e. one for going high and other for going low. how do i merge that into 1 that way i can declare that merged variable as ledPIN
A struct or class could contain the variables (pin number and LED state) and the methods to control a LED.
A class is probably preferable, though in reality, there's very little difference between them.
If you really only want one variable, you could encode the state in a single bit (on/off), and the pin number in the rest of the bits. I suppose you could use a union to do that, or you could just do it the old-fashioned way with more macros and masking.
But that seems an awful faff, when two variables and/or a class/struct make things simpler and easier to read, and maintain.