Hello, I need some help I think I have arduino uno with can bus shield and I want to send data from can h and can L port on shield. The code is below. I am sending 1 data and looped 12 millisecond, its not problem, But I must send 3 data. First data will work and looped 12 millisecond, second data will work and looped 18 millisecond, third data will work and looped 25 millisecond. 3 thread must work. How can I make it. Please help.
problem of code.. I want to write to serial every elapsed 10 seconds and every elapsed 20 seconds and every elapsed 30 seconds. My code is writing only one time 10 , 20, 30 elapsed seconds. Which code I must add ??
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 999; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
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);
if (currentMillis == 10000)
{
Serial.println("10 seconds elapsed");
}
else if (currentMillis == 20000)
{
Serial.println("20 seconds elapsed");
}
else if (currentMillis == 30000)
{
Serial.print("30 seconds elapsed");
}
You want to do something 10, 20, or 30 seconds after you last did something, not 10, 20, or 30 seconds after you started the Arduino. Where are you keeping track of last time for each of the events?
This is 10 seconds from when the Arduino was powered up, it will work once then never again, you need to test the difference between currentMillis and previousMillis.
Personally I would just detect the 10sec interval and use a counter to to stuff on the 1, 2 and 3 counts of that interval.