Show Posts
Pages: [1] 2 3 ... 11
1  Using Arduino / Programming Questions / Re: Two different functions push buttons on: February 18, 2014, 06:23:20 pm
Code:
int led = 13;
int b2 = 2;
int b3 = 3;
int b4 = 4;

boolean keepLightOn = 0;
boolean b4IsPressed = 0;

void setup(){
  pinMode(led, OUTPUT);
  pinMode(b2, INPUT);
  digitalWrite(b2, HIGH);
  pinMode(b3, INPUT);
  digitalWrite(b3, HIGH);
  pinMode(b4, INPUT);
  digitalWrite(b4, HIGH);
}

void loop(){
  if(digitalRead(b2)==LOW or digitalRead(b3)==LOW){
    digitalWrite(led, HIGH);
  }
  else if (keepLightOn == 0){
    digitalWrite(led, LOW);
  }
 
  if(digitalRead(b4)==LOW and b4IsPressed == 0){
    delay(100);                         // debounce
    if(digitalRead(b4)== LOW){          // B4 has changed from unpressed to pressed
      b4IsPressed = 1;
      if(keepLightOn == 0){
        digitalWrite(led,HIGH);
        keepLightOn = 1;
      }
      else{
        digitalWrite(led,LOW);
        keepLightOn = 0;
      }
    }
  }
  if(digitalRead(b4)==HIGH){
    b4IsPressed = 0;
  }
}
 

-Fletcher
2  Using Arduino / Programming Questions / Re: Writing a message and blinking LED for one minute in for loop on: January 22, 2014, 04:53:26 pm
Quote
orry, forgot to say that also. It should go "OFF" after that one minute.  So in short: number should stay printed for 1 minute and LED should stay "ON" for 1 minute, after that 1 minute LED should go "OFF" and the next number should be printed and LED should go "ON" again.

Be careful what you ask for.

Code:
const int ledPin =  13;      
int ledState = LOW;

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 60UL * 1000UL;
int pos = 0;
int myarray[] = {5, 6, 7};

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop(){
  currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    digitalWrite(ledPin, LOW);
    Serial.println(myarray[pos]);
    pos = (pos + 1) % 3;
  }
  digitalWrite(ledPin, HIGH);
}

Don't blame me if it looks like the led is ON all the time  smiley-eek-blue

-Fletcher
3  Using Arduino / Project Guidance / Re: how put a goal to the arduino to reach it on: January 19, 2014, 05:17:46 pm
Quote
for example i want it to get a speed of 3000RPM

Normally I get into my car if I want to get near 3000 rpm ... I have no idea what your question is.

-Fletcher
4  Using Arduino / Programming Questions / Re: Arbitrary precision (big number) library port for Arduino on: January 08, 2014, 06:14:13 pm
Hi pito

http://www.wolframalpha.com/input/?i=cos(0.777777777777777777777777777777777777077777777777777777777777777777777777)

-Fletcher
5  Using Arduino / Programming Questions / Re: What's a good logic to write this? on: January 08, 2014, 04:23:51 pm
Code:
// using If .. else

byte BTN_1 = 8; // Button pin 1
byte BTN_2 = 9; // Button pin 2
byte LED_1 = 10; // Led 1
byte LED_2 = 11; // Led 2
byte LED_3 = 12; // Led 3
byte LED_4 = 13; // Led 4

void setup(){
  pinMode(BTN_1, INPUT);
  digitalWrite(BTN_1, HIGH); // Enable pull-up
  pinMode(BTN_2, INPUT);
  digitalWrite(BTN_2, HIGH); // Enable pull-up
  
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
}

void loop(){
  if(digitalRead(BTN_1) == LOW){     // Button pin 1 is pressed
   if(digitalRead(BTN_2) == LOW){    // Button pin 2 is pressed
     digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, HIGH);
     digitalWrite(LED_4, HIGH);
   }
   else{                 // Button pin 2 is not pressed
     digitalWrite(LED_1, HIGH);
     digitalWrite(LED_2, HIGH);
     digitalWrite(LED_3, LOW);
     digitalWrite(LED_4, LOW);
   }
  }
  else{                  // Botton pin 1 is not pressed
     digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, LOW);
     digitalWrite(LED_4, LOW);
   }
}

Code:
// using switch case

byte BTN_1 = 8; // Button pin 1
byte BTN_2 = 9; // Button pin 2
byte LED_1 = 10; // Led 1
byte LED_2 = 11; // Led 2
byte LED_3 = 12; // Led 3
byte LED_4 = 13; // Led 4

byte mode = 0;

void setup(){
  pinMode(BTN_1, INPUT);
  digitalWrite(BTN_1, HIGH); // Enable pull-up
  pinMode(BTN_2, INPUT);
  digitalWrite(BTN_2, HIGH); // Enable pull-up
 
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
}

void loop(){
  if(digitalRead(BTN_1)==HIGH){mode = 0;}
  if(digitalRead(BTN_1)==LOW and digitalRead(BTN_2) == HIGH){mode = 1;}
  if(digitalRead(BTN_1)==LOW and digitalRead(BTN_2) == LOW){mode = 2;}
 
  switch(mode){
   
    case (0):
     digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, LOW);
     digitalWrite(LED_4, LOW);
     break;
   
    case (1):
     digitalWrite(LED_1, HIGH);
     digitalWrite(LED_2, HIGH);
     digitalWrite(LED_3, LOW);
     digitalWrite(LED_4, LOW);
     break;
   
    case(2):
     digitalWrite(LED_1, LOW);
     digitalWrite(LED_2, LOW);
     digitalWrite(LED_3, HIGH);
     digitalWrite(LED_4, HIGH);
     break;
  }
}
-Fletcher
6  Using Arduino / Programming Questions / Re: Tuning PID control for heating 300 gallon still pot on: January 01, 2014, 05:42:35 pm
Hi oldradio

That is a big still pot you have there.

I would avoid the PID controller for heating such a reactor. The "dead" time is going to be really long. I would do it with a cycles time and a ramping system.
Eg. cycles time = 60 sec.
Below 150F the heater should be on for 100%*cyclus time = 60 sec ON, 0 sec OFF.
Between 150F and 155F: 80%* cyclus time = 48 sec ON, 12 sec OFF.
etc etc.
Above 170F: 0% * cyclus time = 0 sec. ON, 60 sec OFF.
This will give you a nice and easy controllable system for a heater.

But, it's not gonna be enough. You said you wanted to heat 300 gallon to 170F but I think you want to distill a mixture of water and ethanol at atmospheric pressure - am I right?

You can not distill at constant temperature since the composition is changing due to ethanol enriched vapors leaving the pot. The boiling point in the pot is going up due to higher and higher water concentration. You need to set a certain heat input to "drive" the distillation otherwise you will reach the set point and the distillation will stop.

If I were you I would make a control system where you could set the % output to the heater. Just remember to change the output once you get close to the boiling point .... or make a cut-off value that turns off the heating.

Btw, do you have column with condenser and reflux or is this a single step distillation?

-Fletcher
7  Using Arduino / Programming Questions / Re: Millis Accuracy Again on: April 19, 2013, 04:17:39 am
Hi orly_andico

I think it's a software related problem. You are doing some strange things with millis()...

I guess this is your loop:
Code:
void loop() {  
  long tstart = exttimer_millis();

  handler_called++;

  if ((handler_called % THINKPERIOD) != 0) {
    do_autoguider();
  }
  else {
    handler();
  }

  long tcnv = (exttimer_millis() - tstart) + 1;

  if (tcnv < PERIODMILLIS) {
    delay(PERIODMILLIS - tcnv);
  }
}

a delay in main loop and an incremente to keep track of when to do things. That is not how I would have done it.

Code:
void exttimer_init() {
  Timer1.initialize(10000);    // 10 milliseconds
  Timer1.attachInterrupt(exttimer_callback);
}

void exttimer_callback() {
  _mymillis += 10;
}

The exttimer has a resolution of 10 ms. That is alot considering you wanna do tcnv average here:
Code:
void read_encoder(long &A, long &B, long &tcnv) {
  int reading;
  int i;

  long t0, t1;

  t0 = exttimer_millis();

  // this should finish in 5ms or less @ 32ksps
  for (i = 0; i < OVERSAMPLING; i++) {
    reading = read_adc(1);
    A += reading;

    reading = read_adc(2);
    B += reading;
  }

  A = A / OVERSAMPLING;
  B = B / OVERSAMPLING;

  t1 = exttimer_millis();
  
  // tcnv should be in milliseconds
  tcnv = (t0 + t1) / 2;
}

Byt the way. You average calculation is a disaster just waiting to happpen. What happens if you forget to set the variables that &A and &B is refering to to not 0 prior to calling the routine..... strange average .....
Edit: This is happening in the calibrate() where encoderA and encoderB is set to 0 outside the while loop.

Btw, can you pleast explane how the calibrate() routine works. It seemes like a strange way to get the mean values from the encoders and keep the outliers out. It dos not follow dixon's test for outliers.

edit,edit: I have been thinking a but more about that calibration. I think you need to rethink it. You are oversampling from the encoders 64 times and then returning the average as reading value. If this value is an outlire then numerious of the 64 readings must be outliers. You should test for outliers among the crude values returned from the encoders.

-Fletcher
8  Using Arduino / Programming Questions / Re: oil heater in greenhouse on: April 17, 2013, 04:31:28 pm
Hi brygmester

Why do you want the heater (ovn) to go on/off every 250ms. The relay is going to go click, click, click, click every second. The latency time (dĂždtid) in your system is going to far higer than that so why not keep the on/off cycles much higher when you are in the temperature regon where you wanna heat the greenhouse.

It seems like a strange way to lower the heat output to 50% of nominel capacity.

-Fletcher
9  Using Arduino / Programming Questions / Re: Why won't this convert properly?! It is driving me maddddd on: April 12, 2013, 10:48:11 am
Hi

Quote
BTW, what I'm trying to do basically... is count up in binary... to get this pattern:
00000001
00000011
00000111
00001111
00011111
00111111
01111111
11111111

How about this:

Code:
void setup(){
  Serial.begin(115200);
  int lines=8;
  int number=1;
 
  for (int s=lines-1;s>=0;s--){
    for (int t=lines-1;t>=0;t--){
      Serial.print(bitRead(number,t));
    }
    Serial.println();
    number<<=1;  // bitshift number one to the left
    number+=1;   // add 1 to the right most bit
  }
}

void loop(){}

Output:
00000001
00000011
00000111
00001111
00011111
00111111
01111111
11111111


-Fletcher
10  Community / Bar Sport / Re: Ardunio C++ SUCKS!!!! on: August 03, 2012, 09:27:52 am
It was a base safe code .....  smiley-grin

Would be better to change:
Code:
Serial.println("Starting");
    running = 0;

Into:
Code:
Serial.println("Starting");
    running = 1;
too smiley-razz

-Fletcher
11  Community / Bar Sport / Re: Ardunio C++ SUCKS!!!! on: August 03, 2012, 05:03:19 am
Less talk - more action.

Quote
The main program is in a loop looking for a start switch closure. It then calls a custom function (I guess that is a C++ subroutine??) that looks for one of four detector triggers or a stop switch closure, in a loop. Each detector trigger calls one of two different custom functions to energize and de-enrgize some relays. It then returns to the detection process. It only returns to the startup loop if it finds a stop switch closure in the detection process.

I dont's have Arduino, realys, switches at me ... but I did have 8 min left of my lunchbreak.
Code:
const int startSwitch = 12;  // Start switch
const int stopSwitch = 11;   // Stop switch
const int det1 = 10;
const int det2 = 9;
const int det3 = 8;
const int det4 = 7;

boolean running = 0;

void setup(){
  pinMode(startSwitch, INPUT);
  digitalWrite(startSwitch, HIGH);  // enable pull-up
  pinMode(stopSwitch, INPUT);
  digitalWrite(stopSwitch, HIGH);  // enable pull-up
  pinMode(det1, INPUT);
  digitalWrite(det1, HIGH);  // enable pull-up
  pinMode(det2, INPUT);
  digitalWrite(det2, HIGH);  // enable pull-up
  pinMode(det3, INPUT);
  digitalWrite(det3, HIGH);  // enable pull-up
  pinMode(det4, INPUT);
  digitalWrite(det4, HIGH);  // enable pull-up
  
  Serial.begin(115200);
}

void loop(){
  if (digitalRead(startSwitch) == LOW){  // Is it time to start?
    Serial.println("Starting");
    running = 0;
  }
  while (running){
    if (digitalRead(det1) == LOW){action1;}
    if (digitalRead(det2) == LOW){action2;}
    if (digitalRead(det3) == LOW){action3;}
    if (digitalRead(det4) == LOW){action4;}
    if (digitalRead(stopSwitch) == LOW){  // Is it time to stop?
      Serial.println("Stopping");
      running = 0;
    }
  }
}

// ****
void action1(){
  Serial.println("Action 1");
  delay(1000);
}

void action2(){
  Serial.println("Action 2");
  delay(1000);
}

void action3(){
  Serial.println("Action 3");
  delay(1000);
}

void action4(){
  Serial.println("All your base are belong to us");
  delay(1000);
}

Compiled but NOT testet.

-Fletcher

Edit: This code works as intended:
Code:
const int startSwitch = 12;  // Start switch
const int stopSwitch = 11;   // Stop switch
const int det1 = 10;
const int det2 = 9;
const int det3 = 8;
const int det4 = 7;

boolean running = 0;

void setup(){
  pinMode(startSwitch, INPUT);
  digitalWrite(startSwitch, HIGH);  // enable pull-up
  pinMode(stopSwitch, INPUT);
  digitalWrite(stopSwitch, HIGH);  // enable pull-up
  pinMode(det1, INPUT);
  digitalWrite(det1, HIGH);  // enable pull-up
  pinMode(det2, INPUT);
  digitalWrite(det2, HIGH);  // enable pull-up
  pinMode(det3, INPUT);
  digitalWrite(det3, HIGH);  // enable pull-up
  pinMode(det4, INPUT);
  digitalWrite(det4, HIGH);  // enable pull-up
  
  Serial.begin(115200);
}

void loop(){
  if (digitalRead(startSwitch) == LOW){  // Is it time to start?
    Serial.println("Starting");
    running = 1;
  }
  while (running){
    if (digitalRead(det1) == LOW){action1();}
    if (digitalRead(det2) == LOW){action2();}
    if (digitalRead(det3) == LOW){action3();}
    if (digitalRead(det4) == LOW){action4();}
    if (digitalRead(stopSwitch) == LOW){  // Is it time to stop?
      Serial.println("Stopping");
      running = 0;
    }
  }
}

// ****
void action1(){
  Serial.println("Action 1");
  delay(1000);
}

void action2(){
  Serial.println("Action 2");
  delay(1000);
}

void action3(){
  Serial.println("Action 3");
  delay(1000);
}

void action4(){
  Serial.println("All your base are belong to us");
  delay(1000);
}
12  Using Arduino / Programming Questions / Re: retrieve last sent byte over serial on: August 03, 2012, 02:47:27 am
Hi Aconolly

Store values in EEPROM and read from EEPROM during startup.

http://arduino.cc/en/Reference/EEPROM

-Fletcher
13  Using Arduino / Programming Questions / Re: Uploading to What? How to clear? on: August 03, 2012, 02:35:55 am
Code:
void setup(){}
void loop (){}

-Fletcher
14  Using Arduino / Programming Questions / Re: problem with micros() on: August 01, 2012, 02:35:29 pm
Quote
This comes up about twice a month on the forum. The general answer is "you don't". How often do you reset your watch? Practically never, right? That's how often you should be trying to reset the Arduino's timers.

Truth about Arduino:
Anyone who ask:
How can I reset mills or timers?
Does not need the true answer.

By the way wife highjacked the TV so I got this code working:

Code:
const int ScreenPin1 = 11;
const int ScreenPin2 = 12;
const float dist = 1.0;     // Distance between 1 and 2 [ft]

unsigned long time1, time2, deltaTime;
float flySpeed;
boolean running = 0;

void setup(){
  pinMode(ScreenPin1, INPUT);
  digitalWrite(ScreenPin1, HIGH);
  pinMode(ScreenPin2, INPUT);
  digitalWrite(ScreenPin2, HIGH);
  Serial.begin(115200);
  Serial.println("Starting");
}

void loop(){
  if (!running && (digitalRead(ScreenPin1)) == LOW) {
    time1 = micros();
    running = 1;
  }
  if ((running && (digitalRead(ScreenPin2)) == LOW)) {
    time2 = micros();
    deltaTime = time2-time1;
    Serial.print("Time: ");
    Serial.print(deltaTime);
    Serial.println(" us");
   
    flySpeed = dist*1000000UL/deltaTime;
    Serial.print("Speed: ");
    Serial.print(flySpeed);
    Serial.println(" fps");
    Serial.println();
    running = 0;
    delay(1000);
  }
}

-Fletcher
15  Using Arduino / Programming Questions / Re: Writing output values to pins back to back seems buggy, digitalwritefast() too on: August 01, 2012, 03:10:58 am
Hi allanonmage

I don't need a video - just explane in simple words what you want to achive with your program.

I have looked at your program and this is how I interpretate it:

1) Waite for ServoDecode to give READY_state.
2) If READY_state: Set relays to running state.
3) If !READY_state: Set realys to not running state, and prepare for a new run.
4) If running state has been activitated for too long: Set relays to not running state and do nothing forever.

Is this correct?

-Fletcher
Pages: [1] 2 3 ... 11