Fist of I would like to say hello, I have been playing with Arduino's for about 5 yrs now and still today I can not get my head around the IDE code, I guess its not for everybody. So my skill level is nil to very very low, so programming a Arduino with the IDE is about as good as I am. I have been building a "Moody Useless Box" but wanted to add RGB Led's as eye's when the lid open after you flick the switch, when I insert the code where I think it go's and flash to Arduino the led's just turn on after boot yet I have not flicked the switch, for the life of me I cant remember how I did this last time. I did have it working at one time. I have marked the parts I inserted code with //*****// to help. Also removed Sequence 2 to 10 to shrink code to fit post & added pic of setup. any help would be great.
#include <Servo.h>
#include <avr/sleep.h>
// create servo object to control a servo
Servo myservo1; // Lid Servo (8)
Servo myservo2; // Finger Servo (7)
//Servo myservo3; // Flag Servo(4)
// variable to store the servo position
int pos = 0;
int boxon = 1;
// Predefined positions for a spesific box/servo
int S1fra = 1400; // from s1=Servo1
int S1mid = 1800; // Mid
int S1mid2 = 1580;
int S1mid3 = 1640;
int S1til = 1950; // To
int S2fra = 2000; //From s2=Servo2
int S2mid = 1200;
int S2mid2 = 1000; // Almost on the switch
int S2til = 770;
int seq = 0;
void setup()
{
myservo1.attach(8); // attaches the servo on pin 8 to the servo object
myservo2.attach(7); // attaches the servo on pin 7 to the servo object
//myservo3.attach(4); // attaches the servo on pin 4 to the servo object
pinMode(13, OUTPUT); // For the MOSFET
pinMode(2, INPUT); // For the switch interrupt
//************************************************************//
pinMode(9, OUTPUT); //Red
pinMode(10, OUTPUT); //Green // This is the code a added here
pinMode(11, OUTPUT); //Blue
//************************************************************//
digitalWrite(2, HIGH);
//myservo3.write(S3fra);
myservo2.write(S2fra);
myservo1.write(S1fra);
delay(600);
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
// Put all unused pins to input high to save power.
pinMode(12, INPUT);
digitalWrite(12, HIGH);
DDRC = 0; //Analog input 1/6 (PortC) set to input high also
PORTC = 63;
//External interrupt INT0
EICRA = 0; //The low level of INT0 generates an interrupt request
EIMSK = 1; //External Interrupt Request 0 Enable
}
void loop()
{
if (!boxon) //boxon should actually be called boxoff. Did a mistake here.
{
delay(500);
digitalWrite(13, HIGH);
//seq = int(random(0,10)); //If you want random...
// if (seq == 0)Sequense3();
if (seq == 1)Sequense1();
// if (seq == 2)Sequense3();
// if (seq == 3)Sequense9();
// if (seq == 4)Sequense3();
// if (seq == 5)Sequense5();
// if (seq == 6)Sequense3();
// if (seq == 7)Sequense7();
// if (seq == 8)Sequense3();
// if (seq == 9)Sequense2();
// if (seq == 10)Sequense3();
// if (seq == 11)Sequense10();
// if (seq == 12)Sequense3();
// if (seq == 13)Sequense8();
// if (seq == 14)Sequense3();
// if (seq == 15)Sequense6();
// if (seq == 16)Sequense3();
// if (seq == 17)Sequense4();
seq++;
if (seq > 17) seq = 0;
delay(100);
digitalWrite(13, LOW);
boxon = digitalRead(2);
}
else
{
//Set sleep mode, turn off MOSFET and servos
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
//pinMode(4, INPUT);
//digitalWrite(4, HIGH);
pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(7, INPUT);
digitalWrite(7, HIGH);
PRR = 255;
MCUCR |= (1 << BODS) | (1 << BODSE);
MCUCR &= ~(1 << BODSE);
EIMSK = 1;
sleep_mode();
// ZZZzzz...
sleep_disable(); //Awake again...
PRR = 0;
//pinMode(4, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
}
boxon = digitalRead(2); //If pin 2 is low, box is on and bonxon=flase
}
//Fra=From, Til=To (Sorry for some Norwegian variables)
void Sweep(int srv, int fra, int til, int usec)
{
if (srv == 1)
{
if (fra <= til)
for (pos = fra; pos < til; pos += 1)
{
myservo1.writeMicroseconds(pos);
delayMicroseconds(usec);
}
else
{
for (pos = fra; pos >= til; pos -= 1)
{
myservo1.writeMicroseconds(pos);
delayMicroseconds(usec);
}
}
}
if (srv == 2)
{
if (fra <= til)
for (pos = fra; pos < til; pos += 1)
{
myservo2.writeMicroseconds(pos);
delayMicroseconds(usec);
}
else
{
for (pos = fra; pos >= til; pos -= 1)
{
myservo2.writeMicroseconds(pos);
delayMicroseconds(usec);
}
}
}
}
ISR(INT0_vect) // Step rising edge interrupt. Switch flipped.
{
EIMSK = 0; //Turn off interrupt
boxon = digitalRead(2); //Read the switch a couple of times to avoid a nasty non working thing some times.
boxon = digitalRead(2);
boxon = digitalRead(2);
boxon = digitalRead(2);
boxon = digitalRead(2);
boxon = digitalRead(2);
boxon = digitalRead(2);
boxon = digitalRead(2);
boxon = digitalRead(2);
boxon = digitalRead(2);
}
void Sequense1()
{
//************************************************************//
digitalWrite(10, HIGH); // This is the code a added here
//************************************************************//
delay(700);
Sweep(1, S1fra, S1mid, 3000);
delay(1000);
Sweep(1, S1mid, S1fra, 500);
delay(1000);
Sweep(1, S1fra, S1til, 1000);
Sweep(2, S2fra, S2mid, 1800);
Sweep(2, S2mid, S2til, 500);
delay(100);
Sweep(2, S2til, S2fra, 500);
Sweep(1, S1til, S1fra, 500);
//************************************************************//
digitalWrite(10, LOW); // This is the code a added here
//************************************************************//
}
