Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 19, 2013, 12:30:16 am
Code:
#define SENSOR 0  // select the input pin for the
                  // sensor resistor

int val = 0; // variable to store the value coming
             // from the sensor
const int relay = 12;
const int ignition = 11;

void setup() {
 


 Serial.begin(9600); // open the serial port to send
                     // data back to the computer at
pinMode(relay, OUTPUT);                 // 9600 bits per second
pinMode(ignition, INPUT);
}

void loop() {
 
 
 
 { val = analogRead(SENSOR); // read the value from
                            // the sensor

  Serial.println(val); // print the value to
                       // the serial port

  delay(100); // wait 100ms between
              // each send 
 }
 
 if (digitalRead (ignition) == HIGH) {
 
   if (analogRead(SENSOR) <500) {
     digitalWrite(relay, LOW);
     delay(120000);         //delay sensor readings to prevent headlight
   } else {                 //shut off at night from street lights, other cars..ect
     digitalWrite(relay, HIGH);
   }
 } else {
  delay(120000); //keep lights on 2 minutes after ignition off
  digitalWrite(relay, LOW);
  }
}
 
 

Does that "delay(100)" after "serial.println" also need to be changed to millis() or is that a small enough of a delay that it won't make a difference?
2  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 11:57:03 pm
Before moving on you need to fix the original code
Code:
 if (digitalRead (ignition), HIGH)
This will probably work but it is wrong.

Once this is fixed change the program to use millis() for timing.  The BlinkWithoutDelay example in the IDE and http://gammon.com.au/blink will get you started nut feel free to ask for help here.  Once the main program works using millis() you can set about integrating the second one which will be easy once loop() is free running and not blocked by delays.

Well that gammon link is a lot easier for me to interpret than the blinkwithoutdelay example on the arduino website but, I still haven't a clue on how to implement that into my sketch or how to make it conditional to only happen and keep the lights on for 2 minutes after I turn off the ignition key. I spent a long time looking at it and I get the idea easily enough but just don't know how to implement it for anything else but...well, blinking an led
3  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 02:08:05 pm
and do all sketches using millis() use that "if(currentMillis - previousMillis > interval)"
4  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 02:06:04 pm
So should I replace all my sketches using only "int" with "const int" for a more reliable sketch
5  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 04:25:34 am
Do I need one = or the ==? whats the difference between "const int" and just using "int" its kind of ridiculous that there are about a 100 different ways of accomplishing the same end result with arduino. Makes it very hard to learn
6  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 03:35:40 am
Alright so i believe this to be the code I need to control my mosfets. I am totally lost at integrating this code and the one from above together. I was somewhat of a noob but i haven't touched my arduino in months so i'm pretty much right back at square one. The codes I have come up with thus far are bits and pieces of code I put together from codes I implemented months ago in different projects

Code:

int sensorPin = A1;   
int mosfet = 9;     
int val = 0; 

void setup() {
  pinMode(mosfet, OUTPUT); 
}

void loop() {
 
  val = analogRead(sensorPin);
val = map(val,0, 1023, 0, 255);
 
  analogWrite(mosfet, val); 
       
}
7  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 02:44:06 am
well I know when I had my motion sensor lamp going, If you set off the motion sensor again, say a minute after setting it off the first time, it would start/reset the 2 minute cycle all over again before the first one ended . Idk, maybe that was a loop hole in my favor in the code i wrote. I've never used millis() before. Not quite sure how it works. I plan on also using an arduino as a PWM Controller with a POT for mosfets controlling something else entirely in the same vehicle. Would I be able to have the same arduino accomplish both tasks simultaneously?
8  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 02:18:09 am
I've used a 120000 millisecond delay before w/o issue on my uno rev 3 with a motion sensor lamp I made. Other than that how does the code look? I should probably put that first IF statement after the serial monitor command though I suppose?
9  Using Arduino / Programming Questions / Re: Simple Headlight Control Code on: December 18, 2013, 02:04:18 am
sorry haha, the website was taking forever to upload my thread so I had to reset the page and i guess it posted it twice
10  Using Arduino / Programming Questions / Simple Headlight Control Code on: December 18, 2013, 01:46:22 am
I'm thinking about making my car headlights turn on/off automatically for day/night with an additional 2 minute delay after I turn off the car.  The code I made up seems pretty solid but would like someone else to look at it to be safe. I might have my sensor values and relay low/high values mixed around but i'm not worried about that till i bench test it. Just want to know if the code will work

Code:
// Example 07: Send to the computer the values read from
// analogue input 0
// Make sure you click on "Serial Monitor"
// after you upload
//
// Copy and paste this example into an empty Arduino sketch

#define SENSOR 0  // select the input pin for the
                  // sensor resistor

int val = 0; // variable to store the value coming
             // from the sensor
int relay = 12;
int ignition = 11;

void setup() {
 


 Serial.begin(9600); // open the serial port to send
                     // data back to the computer at
pinMode(relay, OUTPUT);                 // 9600 bits per second
pinMode(ignition, INPUT);
}

void loop() {
 
  if (digitalRead (ignition), HIGH) {
 
 { val = analogRead(SENSOR); // read the value from
                            // the sensor

  Serial.println(val); // print the value to
                       // the serial port

  delay(100); // wait 100ms between
              // each send 
 }
 
   if (analogRead(SENSOR) <500) {
     digitalWrite(relay, LOW);
     delay(120000);         //delay sensor readings to prevent headlight
   } else {                 //shut off at night from street lights, other cars..ect
     digitalWrite(relay, HIGH);
   }
  } else {
  delay(120000); //keep lights on 2 minutes after ignition off
  digitalWrite(relay, LOW);
  }
}
 
 
11  Using Arduino / General Electronics / Re: Using Mosfet For PWM In HHO Generator on: December 17, 2013, 01:55:18 am
I found a better mosfet http://www.ebay.com/itm/331085945741?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1423.l2649 uses a 3-5 volt gate drive and someone else has successfully used 3 of them with an arduino at 20 amps with little to no heat using a 45mm heat sink and a same sized fan. Now is just the question of controlling the frequency
12  Using Arduino / General Electronics / Re: Using Mosfet For PWM In HHO Generator on: December 16, 2013, 02:56:13 pm
Here is one from the Playground:
http://playground.arduino.cc/Code/PwmFrequency

Doing an analogRead of the pot, then using that value to set the value should be absolutely doable.


It looks like the answer  to your question is "yes it can be done, but it is a little messy".


Thank you for answering that, but define "messy". I've only been playing with arduino for a year now and i can't make heads or tails of this code

Code:
/**
 * Divides a given PWM pin frequency by a divisor.
 *
 * The resulting frequency is equal to the base frequency divided by
 * the given divisor:
 *   - Base frequencies:
 *      o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
 *      o The base frequency for pins 5 and 6 is 62500 Hz.
 *   - Divisors:
 *      o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
 *        256, and 1024.
 *      o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
 *        128, 256, and 1024.
 *
 * PWM frequencies are tied together in pairs of pins. If one in a
 * pair is changed, the other is also changed to match:
 *   - Pins 5 and 6 are paired on timer0
 *   - Pins 9 and 10 are paired on timer1
 *   - Pins 3 and 11 are paired on timer2
 *
 * Note that this function will have side effects on anything else
 * that uses timers:
 *   - Changes on pins 3, 5, 6, or 11 may cause the delay() and
 *     millis() functions to stop working. Other timing-related
 *     functions may also be affected.
 *   - Changes on pins 9 or 10 will cause the Servo library to function
 *     incorrectly.
 *
 * Thanks to macegr of the Arduino forums for his documentation of the
 * PWM frequency divisors. His post can be viewed at:
 *   http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
 */
void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x7; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}
13  Using Arduino / General Electronics / Re: Using Mosfet For PWM In HHO Generator on: December 15, 2013, 10:43:21 pm
Did you notice in the fine print that at 12v it is only capable of 480 watts? plus i want to control frequency using arduino, speaking of which, no one has told me if i can use a pot to adjust pwm frequency on arduino given the right code. It feels better building things yourself lol
14  Using Arduino / General Electronics / Re: Using Mosfet For PWM In HHO Generator on: December 15, 2013, 10:12:32 pm
alright so I looked at one of these gate drivers, So do i just need to hook the power and ground, then pwm input from arduino then pwm output to mosfet? can i use more than one mosfet per chip? user dc42, does that mosfet http://uk.farnell.com/nxp/buk652r3-40c/mosfet-n-ch-40v-120a-sot78/dp/1863285 that you recommended have a low voltage drop? could I use 3 or 4 of them in parallel just for safe measure and to ensure no overheating all off of the same gate driver? I was looking at this gate driver http://www.ebay.com/itm/TC4421-A-MOSFET-IGBT-10A-Gate-Driver-Inverting-TC4421A-TC4421AVPA-/181069759903?pt=LH_DefaultDomain_0&hash=item2a2899459f
15  Using Arduino / General Electronics / Re: Using Mosfet For PWM In HHO Generator on: December 15, 2013, 12:57:51 am
as far as the 10v gate voltage, what if i used a small step up transformer to bring the arduino pin up from 5 to 10 volts, but i'd prefer not doing it this way
Pages: [1] 2 3 4