For a project containing a led-strip controller I want to replace the IR remote with an Arduino nano. Now after doing some research (http://blog.allgaiershops.com/2012/05/10/reversing-an-rgb-led-remote/) and measurements I found that the led-strip controller uses the NEC protocol.
Now I have tried to create a program that will emulate one of the button press events as measured on the IR_receiver. However, when the output on the digital pin 2 (which is PWM supported) is measured, the timing isn't anywhere near desired.
However, if I use the for loop as written in the functions (high/low output) in the main loop it is possible to create a high output for 500us.
I was wondering if you could give me some support in order to get the code functioning, or could you give me some tips as how to handle timing on the Arduino in an efficient way.
/*
Timer2_Counter_display_time_elapsed.ino
Timer2 Counter Basic Example - Demonstrates use of my Timer2_Counter, which is a timer function with 0.5us precision,
rather than 4us precision like the built-in Arduino micros() function has.
By Gabriel Staples
Visit my blog at http://electricrcaircraftguy.blogspot.com/
-My contact info is available by clicking the "Contact Me" tab at the top of my blog.
-Please support my work & contributions by buying something here: https://sites.google.com/site/ercaguystore1/
My original post containing this code can be found here: http://electricrcaircraftguy.blogspot.com/2014/02/Timer2Counter-more-precise-Arduino-micros-function.html
Written: 17 May 2014
Updated: 30 May 2014
*/
//CODE DESCRIPTION:
//This code demonstrates the use of my Timer2, which provides a more precise timer than micros().
//micros() has a precision of only 4us. However, Timer2 keeps track of time to a precision of 0.5us.
//This is especially important in my code which reads an RC receiver PWM signal, which varies from 900~2100us.
//Though this code demonstrates the use of the Timer_2 functions I have written, it does not adequately demonstrate the
//real utility of the code, so I will state the following:
//By using my Timer2 timer to measure the PWM high time interval on an RC receiver, in place of using micros(), I can get repeatable
//pulse width reads with a fluctuation of ~1us, rather than having a read-in range fluctuating by as much as +/- 4~8 us when I use micros().
//This is an increase in precision of ~8x.
//include the library
#include <eRCaGuy_Timer2_Counter.h>
#define pin_output 2
#include <time.h>
//Note: an object of this class was already pre-instantiated in the .cpp file of this library, so you can simply access its methods (functions)
// directly now through the object name "timer2"
//eRCaGuy_Timer2_Counter timer2; //this is what the pre-instantiation line from the .cpp file looks like
boolean on=false;
int values[24][32]={0};
int one_output =1680;
int zero_output= 560;
void low_output(int );
void high_output(int );
void start_protocol(int);
void end_protocol(int);
void setup() {
//configure Timer2
timer2.setup(); //this MUST be done before the other Timer2_Counter functions work; Note: since this messes up PWM outputs on pins 3 & 11, as well as
//interferes with the tone() library (http://arduino.cc/en/reference/tone), you can always revert Timer2 back to normal by calling
//timer2.unsetup()
//values[0]={8,4,1,5,8,6};
//prepare serial
Serial.begin(115200);
pinMode(pin_output, OUTPUT);
digitalWrite(pin_output, HIGH);
//Output a header of info:
/*Serial.println(F("Notes:"));
Serial.println(F("micros() has a precision of 4us"));
Serial.println(F("get_count() with unsigned long final data type has a final precision of 1us, and is fast"));
Serial.println(F("get_count() with float final data type has a final precision of 0.5us, and is not quite as fast"));
Serial.println(F("get_micros() has a precision of 0.5us, and is slower than the above 2 methods, so one of the above 2 methods is preferred"));
Serial.println(F("=============================================="));*/
}
void loop() {
//declare local variables
delay(2000);
start_protocol();
low_output(8);
high_output(4);
low_output(1);
high_output(5);
low_output(8);
high_output(6);
end_protocol();
static unsigned long t_start = timer2.get_count();
// unsigned long t_micros = micros();
unsigned long t_T2_count = timer2.get_count();
//float t_T2_micros = timer2.get_micros();
if ((t_T2_count - t_start)/2 >= 2000003)
{
digitalWrite(pin_output, HIGH);
}
} //end of loop()
void low_output(int xtimes)
{
for (int i =0 ; i<xtimes ; i++){
static unsigned long t_start = timer2.get_count(); //units of 0.5us; the count accumulated by Timer2_Counter
//acquire time stamps
unsigned long t_T2_count = timer2.get_count(); //units of 0.5us; the count accumulated by Timer2_Counter
//See if 1.000003 seconds has elapsed. If so, print out the time stamps. Note: I am using this elapsed time because I want it to NOT be divisible by 4, so that
//you can hopefully see the extra precision provided by the Timer2_Counter library, which the default Arduino micros() function does not have
if ((t_T2_count - t_start)/2 >= zero_output)//1000003) //if 1.000003 seconds has elapsed
{
t_start = t_T2_count; //update start time
if(on==false) {
digitalWrite(pin_output, LOW);
on=true;
}else {
digitalWrite(pin_output, HIGH);
on=false;
}
}
}
return;
}
void high_output(int xtimes)
{
for (int i =0 ; i<xtimes ; i++){
static unsigned long t_start = timer2.get_count();
//acquire time stamps
unsigned long t_T2_count = timer2.get_count();
if ((t_T2_count - t_start)/2 >= one_output)
{
t_start = t_T2_count; //update start time
if(on==false) {
digitalWrite(pin_output, LOW);
on=true;
}else {
digitalWrite(pin_output, HIGH);
on=false;
}
}
}
return;
}
void start_protocol(){
static unsigned long t_start = timer2.get_count();
unsigned long t_T2_count = timer2.get_count();
digitalWrite(pin_output, LOW);
t_start = timer2.get_count();
t_T2_count = timer2.get_count();
if ((t_T2_count - t_start)/2 >= 9000)
{
digitalWrite(pin_output, HIGH);
}
}
void end_protocol(){
static unsigned long t_start = timer2.get_count();
unsigned long t_T2_count = timer2.get_count();
if ((t_T2_count - t_start)/2 >= zero_output)
{
digitalWrite(pin_output, LOW);
}
t_start = timer2.get_count();
t_T2_count = timer2.get_count();
if ((t_T2_count - t_start)/2 >= 40000)
digitalWrite(pin_output, HIGH);
t_start = timer2.get_count();
t_T2_count = timer2.get_count();
if ((t_T2_count - t_start)/2 >= 9000)
{
digitalWrite(pin_output, LOW);
}
t_start = timer2.get_count();
t_T2_count = timer2.get_count();
if ((t_T2_count - t_start)/2 >= 2100)
{
digitalWrite(pin_output, HIGH);
}
t_start = timer2.get_count();
t_T2_count = timer2.get_count();
if ((t_T2_count - t_start)/2 >= zero_output)
{
digitalWrite(pin_output, LOW);
}
digitalWrite(pin_output, HIGH);
}
For this this code, I used a timing library made by Gabriel Staples.
[update]
After following the tips of #Gmodjackass the following code got the NEC signal on the output of pin 2. note: code can be optimized by adding support for direct pin port manipulation.
#define pin_output 2
#include <time.h>
boolean on=false;
int values[24][32]={0};
int one_output =1680;
int zero_output= 560;
int sb=-1;
void low_output(int );
void high_output(int );
void start_protocol();
void end_protocol();
void selection_protocol(String);
void setup() {
Serial.begin(115200);
pinMode(pin_output, OUTPUT);
digitalWrite(pin_output, HIGH);
Serial.print("user_inpu: on(1) / off(0)");
delayMicroseconds(400);
}
void loop() {
//declare local variables
if (Serial.available())
{
char sb = Serial.read();
Serial.print(sb);
switch (sb){
case '0':
selection_protocol("off");
break;
case '1':
selection_protocol("on");
break;
}
sb=-1;
}
} //end of loop()
void low_output(int xtimes)
{
for (int i =0 ; i<xtimes*2 ; i++){
if(on==false) {
digitalWrite(pin_output, LOW);
on=true;
}else {
digitalWrite(pin_output, HIGH);
on=false;
}
delayMicroseconds(zero_output);
//}
}
return;
}
void high_output(int xtimes)
{
for (int i =0 ; i<xtimes*2 ; i++){
if(on==false) {
digitalWrite(pin_output, LOW);
on=true;
delayMicroseconds(zero_output);
}else {
digitalWrite(pin_output, HIGH);
on=false;
delayMicroseconds(one_output);
}
//}
}
return;
}
void start_protocol(){
digitalWrite(pin_output, LOW);
delayMicroseconds(9000);
digitalWrite(pin_output, HIGH);
delayMicroseconds(4400);
return;
}
void end_protocol(){
digitalWrite(pin_output, LOW);
delayMicroseconds(zero_output);
digitalWrite(pin_output, HIGH);
delay(40);
digitalWrite(pin_output, LOW);
delayMicroseconds(9000);
digitalWrite(pin_output, HIGH);
delayMicroseconds(2100);
digitalWrite(pin_output, LOW);
delayMicroseconds(zero_output);
digitalWrite(pin_output, HIGH);
return;
}
void selection_protocol(String user_input){
if(user_input == "on"){
Serial.print("on selected");
start_protocol();
low_output(8);
high_output(4);
low_output(1);
high_output(5);
low_output(8);
high_output(6);
end_protocol();
}
if(user_input == "off"){
Serial.print("off selected");
start_protocol();
low_output(8);
high_output(4);
low_output(1);
high_output(3);
low_output(1);
high_output(1);
low_output(6);
high_output(1);
low_output(1);
high_output(6);
end_protocol();
}
return;
}
Some of your timing discrepancies may be caused by the very slow nature of arduino's digitalWrite, maybe look into using direct port manipulation. Also consider using this delay function instead since it may cause some more of your timing issues.
(edited for spelling errors)
Related
I am trying to send data from an accelerometer to Java from an Arduino. I am using delta time to limit it to sending only every 250 ms.
The problem is that all the Java program is reading is the first message sent in the setup() over and over.
I added a test Serial.write to check if the program is ever entering the delta time block, and it seems to be sending (or at least, reading) the first 2 characters of that message. The Arduino code is below.
#include <SparkFun_MMA8452Q.h>
int sleepPin = 7;
int stepPin = 6;
int buttonPin = 8;
int stepCount = 0;
boolean stepMode = true;
int delTime = 5000;
MMA8452Q accel; //accelerometer
void setup() {
Serial.begin(9600);
while (millis() < 4000); //wait so I can start java program
Serial.write("Connected");
//set pins
pinMode(sleepPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(stepPin, HIGH); //starts in step mode
delTime = millis() + 250;
}
void loop() {
if (digitalRead(buttonPin) == HIGH) stepMode = !stepMode;
if (millis() > delTime) {
Serial.write("delTime"); //test case
//set led's according to mode
if (stepMode) {
digitalWrite(stepPin, HIGH);
digitalWrite(sleepPin, LOW);
} else {
digitalWrite(stepPin, HIGH);
digitalWrite(sleepPin, LOW);
}
//create string to store data
String data = "";
if (stepMode) data += "s"; //s is step mode key
else data += "z"; //z is sleep mode key
//add actual reading stuff
data += String(accel.getX()) + "," + String(accel.getY());
Serial.write(data.c_str()); //send the lad over
}
}
The Java side is nearly identical (sans the conditions of an if statement, but it doesn't matter because if it doesn't meet the statement it just prints what it sees) to a functional program for serial communication that I've used before. I can include it if necessary though.
The Java console output appears as:
Connected
de
Connected
de
Connected
de
where a new iteration appears about once a second. What am I doing that prevents the Arduino from sending the data?
Not a proper answer yet, more of a test, but I couldn't fit it in a comment.
Changes made:
delTime is now an unsigned long int;
delTime is now reset at the end of the loop();
String object and manipulations were replaced by heap-friendlier code.
Added accel.begin();
Let me know if this works for you, and if not, where it complains. Haven't fully tested the code. You could also try replacing accel.getX() and accel.getY() with numbers; they return short ints, I think.
BTW the button needs debouncing.
#include <SparkFun_MMA8452Q.h>
int sleepPin = 7;
int stepPin = 6;
int buttonPin = 8;
boolean stepMode = true;
unsigned long int delTime = 0;
MMA8452Q accel; //accelerometer
void setup(){
Serial.begin(9600);
while(millis() < 4000); //wait so I can start java program
Serial.write("Connected");
//set pins
pinMode(sleepPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(buttonPin, INPUT);
accel.begin();
digitalWrite(stepPin, HIGH); //starts in step mode
delTime = millis() + 250;
}
void loop() {
char str[15];
if (digitalRead(buttonPin) == HIGH)
stepMode = !stepMode;
if (millis() > delTime) {
//set led's according to mode
if (stepMode) {
digitalWrite(stepPin, HIGH);
digitalWrite(sleepPin, LOW);
Serial.write('s');
} else {
digitalWrite(stepPin, HIGH);
digitalWrite(sleepPin, LOW);
Serial.write('z');
}
sprintf(str, "%d", accel.getX());
Serial.write(str);
Serial.write(',');
sprintf(str, "%d", accel.getY());
Serial.write(str);
Serial.write('\n');
delTime = millis() + 250;
}
}
I'm using an Arduino Uno to control LED. I want the LED to turn on every m seconds and remain ON for n seconds.
I've tried this code using the delay() function (by adding delays after LED is turned ON and OFF) and also using the millis() function (by keeping a track of the time passed since the previous event - ON/OFF). However, in both the approaches, the LED develops a lag of ~1 second after a few (!10) iterations of the ON-OFF cycle. What can I do to increase the accuracy of the time at which the events occur?
int led = 13;
long experimentTime = 240000;
long ledOFFDuration = 8000;
void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
for (int i = 0; i < 100; i++){
digitalWrite(led, HIGH);
delay(10000);
digitalWrite(led, LOW);
delay(10000);
}
}
I've also tried using the watchdog timer (shown below). However, it has the same issue:
#include <avr/wdt.h>
int led = 13;
volatile byte watchDogState = 0b01000000;
volatile int counter = 0;
int counterMax = 5;
bool state = 1;
void setup() {
// put your setup code here, to run once:
wdt_disable();
pinMode(led, OUTPUT);
digitalWrite(led, 1);
setWDT(watchDogState);
}
void loop() {
// put your main code here, to run repeatedly:
// if (time_elapsed == 40){
//state = !state;
//}
}
void setWDT(byte sWDT){
WDTCSR |= 0b00011000;
WDTCSR = sWDT | WDTO_2S;
wdt_reset();
}
ISR (WDT_vect){
counter++;
if (counter >= counterMax){
state = !state;
digitalWrite(led, state);
counter = 0;
}
}
I tried using the port registers directly to avoid using digitalWrite completely. But that doesn't work either. There's a lag of ~5s after 20 minutes using the following code:
int led = 13;
int m = 10;
int n = 10;
boolean on;
long change;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
long now = millis();
if (!on && change < now) {
on = true; //led is now on
change = now + n*1000; //turn off in <n> seconds
PORTB |= B00100000;
}
else if (on && change < now){
on = false; //led is now off
change = now + m*1000; //turn on in <m> seconds
PORTB &= B11011111; // Set pin 4 to 0
}
}
The lag is caused by the digitalWrite-calls. Your processing application waits until digitalWrite has finished and this may take 0.05 seconds or so. To fix your problem I would save the current time in milliseconds.
int led = 13;
int m = 24;
int n = 8;
boolean on;
long change;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
long now = System.currentTimeMillis();
if (!on && change < now) { //led is off and time for change has come
on = true; //led is now on
change = += n*1000; //turn off in <n> seconds
digitalWrite(led, HIGH); //turn on led
} else if (on && change < now) { //led is on and time for change has come
on = false; //led is now off
change = += m*1000; //turn on in <m> seconds
digitalWrite(led, LOW); //turn off led
}
}
the lamp will now instantly turn on on startup, wait n seconds, turn off, wait m seconds and restart from the beginning.
If you want to create a delay at the beginning so that the lamp doesn't get turned on immediately you can simply add this to your setup-function:
change = now + SECONDS*1000;
EDIT
You pointed out that it still gives you lag.
One problem might be that loop() is not run every millisecond. So I maybe got a solution for you.
Replace the following two lines:
change = now + n*1000; //turn off in <n> seconds
...
change = now + m*1000; //turn on in <m> seconds
to this:
change += n*1000; //turn off in <n> seconds
...
change += m*1000; //turn on in <m> seconds
Now it won't take the current time anymore which means that even if loop is only run every second or two it should still not cause any lag.
If this won't work I'm afraid it looks like the timer on the arduino might not be the most precise one. If this is the case try to measure the exact offset and then create a miltiplicator for the time.
I have a simple code with delays.
I'd like to know how to convert this code to millis? Is there a function to do so?
long revers = 1000;
void setup() {
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
}
void loop() {
digitalWrite(D1, LOW);
delay(revers);
digitalWrite(D2, HIGH);
delay(revers);
digitalWrite(D2, LOW);
delay(revers);
digitalWrite(D1, HIGH);
delay(revers);
}
The basic concept is this: record the millis() at a given moment in a variable - say 'starttime'. Now, during every loop(), check the time that has elapsed by subtracting millis() from 'starttime'. If the elapsed time is greater than the delaytime you set, execute code. Reset the starttime to create a repeating pattern.
That may be too short of an explanation for you, so before you dive into the code, I highly advise you to read this introduction about using millis() for timing. It's lengthy, but it explains the principle extensively. This will help you understand the code below.
Lastly, there are several libraries written to make the use of timing easier. For instance the SimpleTimer-library, but you can google "arduino timer library" for others. I've included an example below.
1 second on, 3 seconds off:
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
int fase; //value used to determine what action to perform
void setup() {
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
startMillis = millis(); //initial start time
fase = 0;
}
void loop() {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
if (fase == 0)
{
digitalWrite(8, LOW);
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
fase = 1; //increment fase, so next action will be different
}
else if (fase == 1)
{
digitalWrite(7, HIGH);
startMillis = currentMillis;
fase = 2;
}
else if (fase == 2)
{
digitalWrite(7, LOW);
startMillis = currentMillis;
fase = 3;
}
else if (fase == 3)
{
digitalWrite(8, HIGH);
fase = 0;
startMillis = currentMillis;
}
}
}
Example of a flashing led using the SimpleTimer library
#include <SimpleTimer.h>
// the timer object
SimpleTimer timer;
int ledPin = 13;
// a function to be executed periodically
void repeatMe() {
digitalWrite(ledPin, !digitalRead(ledPin));
}
void setup() {
pinMode(ledPin, OUTPUT);
timer.setInterval(1000, repeatMe);
}
void loop() {
timer.run();
}
I'm trying to see if I can combine tasks that are currently being handled by two separate Arduino Unos onto a single Uno. I was shown the Adafruit guide to "multi-tasking" on the Arduino (Link) and thought I would give it a try.
I feel like I'm missing something really obvious, but my code is just not working...
I'm controlling a series of solenoids. They need to each act based on individual timing. I created a Solenoid class that handles telling the solenoid when to start and then monitor when to close it. For the life of me I can't see where my error is, but I never wind up satisfying my end condition.
class Solenoid {
//Class Variables
int solenoidPin;
long chargeTime;
long ventTime;
bool started = false;
unsigned long startMillis;
unsigned long endMillis;
unsigned long previousMillis;
int solenoidState;
//Constructor
public:
Solenoid(int pin, long charge, long vent) {
solenoidPin = pin;
pinMode(solenoidPin, OUTPUT);
chargeTime = charge;
ventTime = vent;
solenoidState = LOW;
previousMillis = 0;
}
void Start() {
Serial.println("Start called");
started = true;
}
void Update() {
//Check to see if it is time to change the state of the solenoid
unsigned long currentMillis = millis();
Serial.println("Update");
if((started == true) && (currentMillis-previousMillis <= chargeTime)) {
//Run
Serial.print("Run: Charge Time=");
Serial.print(chargeTime);
Serial.print(" current-previous=");
Serial.println(currentMillis-previousMillis);
previousMillis = currentMillis;
} else if ((started == true) && (currentMillis-previousMillis >= chargeTime)){
//Stop
Serial.println("Stop");
}
}
};
//Setup the solenoids
Solenoid solenoid1(13, 70, 70);
void setup() {
Serial.begin(115200);
Serial.println("Ready");
solenoid1.Start();
solenoid1.Update();
solenoid1.Update();
solenoid1.Update();
solenoid1.Update();
solenoid1.Update();
}
I'm just running everything in setup so I can see a few runs.
Can you help me with my clearer stupid mistake?
It's possible that the entire setup() void is executing faster than 70ms, meaning you never get to you end position before the 5 calls to solenoid1.Update() complete.
Try this:
class Solenoid {
//Class Variables
int solenoidPin;
long chargeTime;
long ventTime;
bool started = false;
unsigned long startMillis;
unsigned long endMillis;
unsigned long previousMillis;
int solenoidState;
//Constructor
public:
Solenoid(int pin, long charge, long vent) {
solenoidPin = pin;
pinMode(solenoidPin, OUTPUT);
chargeTime = charge;
ventTime = vent;
solenoidState = LOW;
previousMillis = 0;
}
void Start() {
Serial.println("Start called");
started = true;
}
void Update() {
//Check to see if it is time to change the state of the solenoid
unsigned long currentMillis = millis();
Serial.println("Update");
if((started == true) && (currentMillis-previousMillis <= chargeTime)) {
//Run
Serial.print("Run: Charge Time=");
Serial.print(chargeTime);
Serial.print(" current-previous=");
Serial.println(currentMillis-previousMillis);
previousMillis = currentMillis;
} else if ((started == true) && (currentMillis-previousMillis >= chargeTime)){
//Stop
Serial.println("Stop");
}
}
};
//Setup the solenoids
Solenoid solenoid1(13, 70, 70);
void setup() {
Serial.begin(115200);
Serial.println("Ready");
solenoid1.Start();
}
void loop(){
solenoid1.update()
}
The rest looks fine to me; if you still have issues try using explicit variable declarations instead of your constructor to troubleshoot if that's the issue.
Turns out I was erasing my own counter by setting previousMillis = currentMillis. Once I killed that, it started working. I added some other functionality since then, like a delayed start, but this is primarily the same code.
void Update() {
//Check to see if it is time to change the state of the solenoid
unsigned long currentMillis = millis();
if((started == true) && (currentMillis - startMillis <= startDelay)) {
//keep waiting
Serial.println("Waiting");
} else if((started == true) && (currentMillis - startMillis < (chargeTime + startDelay) )) {
//Run
Serial.print("Run ");
Serial.println(currentMillis - startMillis);
digitalWrite(solenoidPin, HIGH);
} else if ((started == true) && (currentMillis - startMillis >= (chargeTime + startDelay) )){
//Stop
Serial.print("Stop ");
Serial.println(currentMillis - startMillis);
digitalWrite(solenoidPin, LOW);
started = false;
}
}
new here! been recommended many times to come here for help so here I am.
I'm supposed to write a program that allows me to change the rate of a blinking LED light through the comm port. I'm sure this is easy but I've honestly got no clue as I am behind in this class.
anything would help really, i honestly want to learn how to do this, not just come here and get the answer.
thanks in advanced!
// global variables
#include <EEPROM.h>
unsigned long ms_runtime;
int state;
// possible values 0 -> 1 -> 2 -> 3 -> 0
int one_ms_timer;
// define all timers as unsigned long (they are incremented every 100ms = 0.1s)
unsigned long timer1;
unsigned long button_dbnc_tmr = 0;
// timer1 is used for blinking LED
const int LED1 = 13;
// function prototypes
void read_memory(void);
void update_memory(void);
void comm_control(void);
void led_control(void);
void turnoff(void);
void flash_1s(void);
void flash_2s(void);
void flash_3s(void);
void timers(void);
void setup()
{
read_memory();
pinMode(LED1, OUTPUT);
Serial.begin(9600);
//initialize uart
}
void loop()
{
static bool allow_change;
static int counter;
timers();
comm_control();
led_control();
}
void led_control()
{
switch (state)
{
case 0:
turnoff();
break;
case 1:
flash_1s();
break;
case 2:
flash_2s();
break;
case 3:
flash_3s();
break;
}
}
void turnoff()
{
digitalWrite(LED1, LOW);
}
void flash_1s()
{
if (timer1 < 10)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 20)
timer1 = 0;
}
}
void flash_2s()
{
if (timer1<20)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 30)
timer1 = 0;
}
}
void flash_3s()
{
if (timer1<30)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 40)
timer1 = 0;
}
}
void read_memory()
{
timer1 = EEPROM.read(one_ms_timer);
timer1++;
EEPROM.write(one_ms_timer, timer1);
Serial.begin(9600);
}
void update_memory()
{
EEPROM.update(timer1, one_ms_timer);
}
void comm_control(void);
{
char comm_reveier = serial_read;
if (
)
}
void timers(void)
{
if (millis() > (ms_runtime + 1))
{
ms_runtime = ms_runtime + 1;
one_ms_timer++;
}
else if (ms_runtime > millis())
ms_runtime = millis();
if (one_ms_timer > 99) //every 100 ms
{
one_ms_timer = 0;
button_dbnc_tmr++;
timer1++;
}
}
Load the Standard Firmata library on your Arduino board. Then use a library of your choice to build your comm prog. An overview of these can be found here.
Assuming you are using the Arduino IDE, this code sample should give you the general idea of what you need to do:
// pins for the LEDs:
const int ledPin = 13;
// Default blink rate
int rate = 1000;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(ledPin, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int rate = Serial.parseInt();
}
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(rate); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(rate); // wait for a second
}