Which timer or how to time 3 events?

I want to build a simple robot that rotates a barrel painted in 3 different colors red, green and yellow.
so what it should do is:
Time 0 start
Time 2 min, rotate motor and stop @ switch green
Time 11 min, rotate motor and stop @ switch yellow
Time 12 min, rotate motor and stop @ switch red
loop

I used a week now and everything have failed :blush:
But this is my first programming experience so learning a lot.

Any advice appreciated, but i am total noob, so need some explanation :wink:

best regards

Jan

Is the red, green, yellow barrel a substitute for red, green, yellow LEDs?

Can you give us the bigger picture about what you are doing?

Many people use millis() to time things. Have you ever made a sketch using millis()?

Millis returns the number of milliseconds that have gone by since the sketch started. So If you start a sketch, check the millis() amount and then do something after millis() is 120,000 bigger than it was before that will be about 2 minutes?

Does your time have to be exact or is about 2 minutes good enough?

Is your project battery powered or mains powered?

"Everything have failed" is not at all helpful. How have you failed? You dont' know how to wire things up? You can't get sketches to run? You don't know how to write code? What exactly are you you having problems with?

More information leads to better answers.

Is the red, green, yellow barrel a substitute for red, green, yellow LEDs?

It is for timing surfing heats.
Red means heat ended, green go and yellow heat soon ending. Since it is battery powered project on the beach where sun light makes lamps hard to see, i am turning a big barrel with the 3 colors painted on 1/3 each....

Many people use millis() to time things. Have you ever made a sketch using millis()?

Millis returns the number of milliseconds that have gone by since the sketch started. So If you start a sketch, check the millis() amount and then do something after millis() is 120,000 bigger than it was before that will be about 2 minutes?

Does your time have to be exact or is about 2 minutes good enough?

I think ican live with the millis precision.

I tried this first:

if (elapsed_time > trans) {digitalWrite(Flag_motor, HIGH);}

it kind of worked, the timing part worked and it went HIGH when it should.

Then i have 3 switches for stopping in the right position:

if (green == HIGH) {
digitalWrite(Flag_motor, LOW);}//input on pin 2

It works it stops the motor. The problem is both statements locks up the system bye keeping high or low

Then i tried the digitalToggle instead of digitalWrite in the same code, but it is acting up. My output for Flag_motor (pin 13) meassures 2.4V instead of 0V or 5V. It then toggles to 0v and 5 V when the input green (pin 2) is set high...

I have looked at most of the timers, but everybody is blinking away in there sketches and being mathematically and data logically chalenged, i have not found anything that i could convert to my needs.

So a simple toggle solution that works then i can program with simple if statements i can understand :wink:

But it will probably give me problems with how it loops.....
Best regards

Jan

"big barrel", seen from a surfboard... hmmm. We aren't talking 5v hobby motors. 12v? 24v? Car batteries? Wheelchair motors?
You are going to have people start talking about stepper motors and how you control how far the barrel rotates.

"Then i have 3 switches for stopping in the right position" is going to require a lot more explanation. Do you have like a cog or protrusion on the barrel that physically strikes a momentary switch on rotation? Are you reading the reflectivity change in colors with an infrared LED? Do you have a magnet activated reed switch? Are you just running a motor for a certain amount of time and hoping for the best? Or what?

But that isn't what you seem to be stuck on. You seem to be having programming issues.
The way to proceed with that is to post your code and let people take a look.
If you just want things to run once, run it all in the set up and leave the loop empty.

Problem is we can't really help with the code too much without knowing more about the physical set up.
More info about your set up makes code writing easier.

It is a setup with a 12 V deep cycle battery and a windschield wiper motor, i have 3 micro switches 120° apart, a nob on the axle, it runs as it should if i use the switches for breaking the relays....so mechanically i am all good. I know servo motor would have been number one, but i am on no budget :wink:

This is my old aproach with 555 timers abandoned it when it was time for time tuning, because i want 5-15 min adjustable timing at least :

if ((elapsed_time > trans) && (red == LOW)) {
digitalWrite(flag_motor, HIGH);

This was my latest attempt, but flag_motor still dos not go back to low after the red switch is activated...

My problem is that all i do locks the output in low or high, i need to find out how to toggle.

Another aproach is using While, but i have not figured out how to run a timer with While ....

Best regards

Jan

if ((elapsed_time > trans) && (red == LOW)) {
digitalWrite(flag_motor, HIGH);

This was my latest attempt, but flag_motor still dos not go back to low after the red switch is activated...

This is really quite meaningless without context and explantion. Elapsed_time and trans we can guess are variables for working with millis() values. Red being low means what? That red is showing? That the nub on the axle is pressing a switch (usually when switches are pressed things are high)?

Breaking relays? They don't work if they are broken....

The way to proceed with that is to post your code sketch and let people take a look.

I will post my code

//Sample using LiquidCrystal library
#include <LiquidCrystal.h>

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//Output for motor setup,Horn, stop switchs
int flag_motor = 13; //output for relay for motor
int red_stop = 2; //input for switch for stoping motor in the right position.
int red; //variabel for reading red_stop pin 2
//Variables defined
long cycle_start; //time reference
long elapsed_time; // time since start of cycle
int heat_min = 10; //variabel for changing heat_time in min
long heat_time = (heat_min * 1000); //the duration off timed event nr 2 the duration of a heat…set to 1s while debugging
int trans_min = 4; //variabel for changing trans in min this is timed event nr 1 and is the transition time between heats…
long trans = trans_min * 1000;//the duration of timed event nr 1, set to 1s while debugging
//int green; //variabel for reading pin 3
int heat_no = 0; //variabel for counting heats.
int heat_change = 0; // variabel current state for HEAT_NO
int lastheat_change = 0; //variabel previous state for HEAT_NO

void setup()
{
cycle_start = millis(); //start time
Serial.begin(9600); // set up Serial library at 9600 bps
lcd.begin(16, 2); // start the LCD library

lcd.setCursor(8,0); //set position on LCD
lcd.print(“Heat:”); // print Heat

pinMode (flag_motor, OUTPUT); //set pin 13 as output

pinMode (red_stop, INPUT); //set pin 2 as input (stop switch)

}

void loop(){

elapsed_time = millis() - cycle_start;//sets up MY timer

red = digitalRead(red_stop); //variabel til aflæsning af pin 2
lcd.setCursor(5,0); //set position on LCD
lcd.print(red); // print V

//this code is for counting heats, it is a copy of StateChangeDetection.
heat_change = digitalRead(red_stop);//

if (heat_change != lastheat_change) {
if (heat_change == HIGH) {
heat_no++;
}
}
lastheat_change = heat_change;

lcd.setCursor(14,0); //set position on LCD
lcd.print(heat_no); // prints the value of heat_no

lcd.setCursor(12,1); // move cursor to second line “1” and 9 spaces over
lcd.print( (int)(elapsed_time / 1000L)); //prints time in seconds
//lcd.setCursor(15,1); // move cursor to second line “1” and 9 spaces over
//lcd.print(“ss”); //for future use when time is printed in min and sec.

//1st timed event, problem is the else statement locks the pin 13 LOW, do not now how to make it work?
if ((elapsed_time > trans) && (red == LOW)) {
digitalWrite(flag_motor, HIGH);
}
else

{digitalWrite(flag_motor, LOW);

}

}

So there is the code…

I see from your double post of this over in programming that you have given up and moved on.

If you should see this and revisit the project at a future point, my suggestion would be to split your coding up.

I would make a motor function (test it as separate code and make sure it is reliable and works) that turns on the motor and advances the barrel one color stop. That's all it does. Write motor high, when stop switch is triggered, write motor low. Done.

Barrel starts at red.

Then for the main loop I would have my timers.
After 2 minutes, call motor function. Advances barrel one stop, barrel at green.
After 9 minutes, call motor function. Advances barrel one stop, barrel at yellow.
After 1 minute, call motor function. Advances barrel one stop, barrel at red.
Loop.

If I got that running, then I would try to get fancy and output heat numbers and a running clock to an LCD screen.

Best of luck.