I'm trying to control a RGB led and have it change color 1 time every 3 seconds. But because of the delay in the loop it will create a gap in witch it will not be able to read my sensors. In this case it's extremely important the sensors can be read within a few milliseconds.
int PIN_RED = 9;
int PIN_GREEN = 10;
int PIN_BLUE = 11;
int motor = [4,5]; //4 backward, 5 forward
int tButton= 2; //tells motor to stop then go backward
int bButton= 3; //tells motor to stop then go forward
//Colors
#define BLACK B000
#define RED B001
#define GREEN B010
#define YELLOW B011
#define BLUE B100
#define MAGENTA B101
#define CYAN B110
#define WHITE B111
#define WAIT 1000 // 1000ms (1s) delay
byte pattern[] = {
RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA
};
void color(byte v)
{
digitalWrite(PIN_RED, v & 0x01); // write current values to LED pins
digitalWrite(PIN_GREEN, (v >> 1) & 0x01);
digitalWrite(PIN_BLUE, (v >> 2) & 0x01);
}
void setup()
{
pinMode(PIN_RED, OUTPUT); // sets the RGB pins as output
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
pinMode(motor, OUTPUT);
pinMode(tButton,INPUT);
pinMode(bButton,INPUT);
}
// Main program
void loop()
{
for (byte i = 0; i < 6; i++) {
color(pattern[i]);
delay(3000); // this is where i need to be able to read 1 of the 3 touch sensors I will have.
}
}
that delay is my problem is there a way i can still let the if statements run , while that delay is still going? because that delay will make up a big majority of the loop.
int PIN_RED = 9;
int PIN_GREEN = 10;
int PIN_BLUE = 11;
int counter= 0;
int animationDelay = 200;
int Motor = [4,5]; //4 backward, 5 forward
int Button1= 2; //tells motor to stop then go backward.
int Button2= 3; //tells motor to stop activated by chain dog.
int Button3= 6; //tells motor to run forward (activated by car)
// Matt -- My added in variable declariations
long savmill = 0
byte state = 0
//Colors
#define BLACK B000
#define RED B001
#define GREEN B010
#define YELLOW B011
#define BLUE B100
#define MAGENTA B101
#define CYAN B110
#define WHITE B111
#define WAIT 1000 // 1000ms (1s) delay
byte pattern[] = {
RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA
};
void color(byte v)
{
digitalWrite(PIN_RED, v & 0x01); // write current values to LED pins
digitalWrite(PIN_GREEN, (v >> 1) & 0x01);
digitalWrite(PIN_BLUE, (v >> 2) & 0x01);
}
void setup()
{
pinMode(PIN_RED, OUTPUT); // sets the RGB pins as output
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
pinMode(Motor,[4,5] OUTPUT); //4 is backward 5 is forward
pinMode(Button1 , INPUT);
pinMode(Button2 , INPUT);
pinMode(Button3 , INPUT);
savmill = millis();
}
// Main program
void loop()
{
unsigned long curmill = millis();
if (curmill - savmill > WAIT) {
savmill = curmill;
state = (state MOD 6) + 1;
color(pattern[state]);
}
if Button3 = (HIGH){ // runs motor
Motor ,5 = (HIGH);
}
if (Button1=(HIGH){ // stops motor
Motor , 5 =(LOW
Curmil - savmill > WAIT) // delays the motor without stopping the RGB code.
Motor , 4= (HIGH)
}
if Button2=(HIGH){// stops motor until being reactivated.
(motor , 4 (LOW))
}
}
Once you get over the learning hump for variables and the like and come back to this problem, you may consider using a timer process to read the sensor and update global variables. You can then use those variables in your loop without a worry about delay time.
See the code at the bottom and notice the callback function. In that callback function you can read your sensor / buttons and set variables as needed. This frees up your loop to do whatever "primary" processing needed.
You may want to start with that code, have it read a button state and set a variable accordingly. You can then, in the loop, turn set the LED status (on/off) based on that variable. That may be a good start to accomplishing the desired task and provide an easier ramp up as far as that debugging is required. First get your button pressing / sensor reading code working before you attempt to integrate it into a larger or existing process.
Some working code (old and untested,uses Timer 2, but should work)
//Find timer at http://www.arduino.cc/playground/Main/MsTimer2
#include <MsTimer2.h>
//--- Button Pin
#define BUTTON 7
//--- used to store old value
int old_val=0;
unsigned long time=0;
boolean buttonPressNew = false;
void checkButton(){
//IMPORTANT: NO DELAY OR LONG ACTION HERE .. just change button status and take action in the loop
//-- no need for val to be global .. just takes up 2 bytes for nothing being global.
int val = digitalRead(BUTTON);
//-- If they are not the same then ...
if( val != old_val ){
//-- Reset last value
old_val = val;
//-- If value is turned on .. then we need to tell the system it was hit once ..
// but not do it over and over until released
if( val ){
buttonPressNew = true;
}
}
}
void setup() {
//--- Starndard Setup
pinMode(BUTTON,INPUT);
Serial.begin(9600);
//--- Initialize timer2 .. using 20ms for this example
MsTimer2::set(20, checkButton);
MsTimer2::start();
}
void loop() {
//Do any normal processing here ... it can run as long as you like and not effect the reading of the button.
//Simple Example ... a delay .. this will only process a single button click.
// so in this example .. if the button is pressed twice in this delay period .. it picks up only one time.
// if the button check was in the loop .. it would not see the button at all.
// if multiple button presses being excepted while in this delay .. and the action is quick .. it can be run in the loop
// or a counter added
delay(5000);
// This flag is set when a button is pressed .. take action in the loop and reset
if( buttonPressNew ){
// Take action based on the button being pressed...
// then reset
buttonPressNew = false;
//IMPORTANT: Notice serial printing done here .. not in timer process
Serial.println("Button Was Pressed");
}
}
//Jeromy's universal Cable lift.
/* You need 3 momentary switches a ss car can easily press and an H bridge taht can run a knex/custom motor*/
int PIN_RED = 9;
int PIN_GREEN = 10;
int PIN_BLUE = 11;
int counter= 0;
int motorForward = 4;
int motorBackward = 5;
int Button1= 2; //tells motor to stop then go backward.
int Button2= 3; //tells motor to stop activated by chain dog.
int Button3= 6; //tells motor to run forward (activated by car)
// Matt -- My added in variable declariations
long savmill = 0
byte state = 0
//Colors
#define BLACK B000
#define RED B001
#define GREEN B010
#define YELLOW B011
#define BLUE B100
#define MAGENTA B101
#define CYAN B110
#define WHITE B111
#define WAIT_LED 6000 // 5000ms (10s) delay
byte pattern[] = {
RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA
};
void color(byte v)
{
digitalWrite(PIN_RED, v & 0x01); // write current values to LED pins
digitalWrite(PIN_GREEN, (v >> 1) & 0x01);
digitalWrite(PIN_BLUE, (v >> 2) & 0x01);
}
void setup()
{
pinMode(PIN_RED, OUTPUT); // sets the RGB pins as output
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
pinMode( motorForward, OUTPUT );
pinMode( motorBackward, OUTPUT );
pinMode(Button1 , INPUT);
pinMode(Button2 , INPUT);
pinMode(Button3 , INPUT);
savmill = millis();
}
// Main program
void loop()
{
unsigned long curmill = millis();
if (curmill - savmill > WAIT_LED) {
savmill = curmill;
state = (state MOD 6) + 1;
color(pattern[state]);
}
switch(motorState)
{
switch(motorState)
{
case STATE_NOTHING:
if (button1 == HIGH)
{
motorState = STATE_FORWARD;
motorForward = HIGH;
}
break;
case STATE_FORWARD:
if (button2 == HIGH)
{
motorState = STATE_WAITING;
motorForward = LOW;
time_start = millis();
}
break;
case STATE_WAITING:
time_now = millis();
if ( time_now - time_start >= WAIT_MOTOR )
{
motorState = STATE_BACKWARD;
motorBackward = HIGH;
}
break;
case STATE_BACKWARD:
if ( button3 == HIGH )
{
motorState = STATE_NOTHING;
motorBackward = LOW;
}
break;
}
}