Arduino code not working - arduino

My plan is to make an adjustable speed strobe. I'm just learning to code and this is what I have so far.
int potentiometer_pin = A0;
int led_pin = 7;
int on_time = 100;
int analog_value_multiplier = 15;
int strobe_delay = 0;
int minimum_delay = 500;
void setup() {
pinMode(led_pin, OUTPUT);
}
void loop() {
strobe_delay = minimum_delay + analogRead(potentiometer_pin) * analog_value_multiplier;
digitalWrite(led_pin, HIGH);
delayMicroseconds(on_time);
digitalWrite(led_pin, LOW);
delayMicroseconds(strobe_delay - on_time);
}
I have the LED + on digital 7 with a 220ohm resistor and the pot on analog 0, it is a 10K pot with one side hooked up to 5v+ and the other to ground. My problem is that the LED stays on and turning the pot just changes the brightness. Any help on what to do - not just a new code but what to do? I want to actually learn how to fix this.

like david said, but I will add I think you want delay not delayMicroseconds.
http://arduino.cc/en/Reference/delay

Your speeds are all WAY too fast. Multiply all your delays by about 100. You've made a pulse width modulator.

Related

Why does the ledstrip stop partway and then go all the way and can I make this more efficient

So I need some help reviewing and editing the code below. Is there a way to make it more efficient instead of what I did? Also, are there better ways to do some of the methods I did? Answers are appreciated greatly (I'm fairly new to Arduino).
The code below is for a corridor ledstrip that lights up when the distance sensor on the Arduino detects anything below a certain distancenear it (yes, I thought of using a motion sensor but I only had distance sensors on hand).
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;
#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 60
int timingnum = 0;
//#define BRIGHTNESS bright
int bright = 100;///the brightness of the leds
int lednum = 60;///the number of leds available
int timer;
CRGB leds[NUM_LEDS];
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
// FastLED.setBrightness( BRIGHTNESS );
}
void loop() {
lights();
}
void sensor() { ///THIS IS CLEAN
digitalWrite(trigPin, LOW);
delayMicroseconds(timingnum);
digitalWrite(trigPin, HIGH);
delayMicroseconds(timingnum);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
}
// in the lights the reason why it wouldnt output the data for the
// represented while loops is because the while loop continued running
// without the data from the distance and thus it needs to constantly
// be updated in the while loop conditional statments
void lights() {
sensor(); //data update
while (distance <= 10) {
for (int x = 0; x <= 60; x++) {
leds[x] = CRGB(255,255,255);
FastLED.show();
sensor(); //sensor update
}
for (timer = 0; timer <=800; timer++) {
sensor();
}
sensor(); //replaces the data updates above
}
sensor(); //sensor update
while (distance >= 11) {
for (int x = 0; x <= 60; x++) {
leds[x] = CRGB(0,0,0);
FastLED.show();
}
sensor(); //data update
}
}
I would be tempted to make a little function to set the RGB colors of your LED strip: void fillup(byte r, byte g, byte b). Then you would call it in two places: fillup(0,0,0) and fillup(127,127,127) (yeah, not 100% on).
Also, I'm confused why you have so many sensor() calls in the first while loop. Seems like you only need to call it once when you need updated values for distance.
Also, the comments on the sensor calls are confusing... after reading them I have no idea what sensor is supposed to do. I tend to put a comment before functions that describes what they do, and put a stand-alone comment in the code to explain what the next "paragraph" does. And I avoid banners - they get in the way.
Do you want FastLED.show() inside the loop that updates the colors? Or just after the loop, to update the hardware after the array is changed? IDK, just asking.
I usually do not mind globals, but in this case you would be better off letting sensor return the distance. Then you could while ( sensor() <= 10 ). Or use a do.. while loop with one call to sensor at the bottom if you want to keep the global.
I'd try to get rid of the floating point, too... just calculate what your thresholds are in the raw echo values, and use those. Do we really care what the internal units of measurement are?
Sorry for the long unload... There's nothing wrong with what you have.

Arduino IR controlled light fade issue

Never coded before. I'm having trouble adding a fade feature to this code. I have added a limit of 50. I want the LED to become dimmer the closer the limit. However, the light does cut off after 50 which is what I am also looking for. I am using a SHARP 2Y0A02 F 26 IR sensor to measure the distance.
I have played around with different IF loops and have had no success. When I tried the code without the limit it would fade quite well, however I need the same effect within the limit.
#define sensor A0 // Sharp IR 2Y0A02 F 26
int led = 6;
int brightness = 0;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
// 5v
float volts = analogRead(sensor) * 0.0048828125; // value from sensor * (5/1024)
int distance = 9462/(analogRead(sensor) - 16.92); // Distance in CM
int fade = distance;
delay(1000); // slow down serial port
if (distance <= 50) {
brightness = -distance;
}
else {
brightness = 0;
}
Serial.println(distance); // print the distance
}
I expect the light to be very dim at 50 and be gradually brighten when the object is getting closer to 0.

Arduino infiniti tone

I am making a smoke detector.
When it detects smoke iz should alert with buzzer.
Is there any way I could make it to buzz forever until external interupt such as restart pin?
Or could I just remove timing from tone() function.
Here is the code I use.
int sensorValue;
int digitalValue;
int green = 8;
int red = 7;
void setup(){
Serial.begin(9600);
pinMode( 0, INPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
}
void start(){
digitalWrite(green, HIGH);
}
void loop() {
sensorValue = analogRead(0);
digitalValue = digitalRead(0);
Serial.println(sensorValue,DEC);
Serial.println(digitalValue,DEC);
delay(2000);
if(analogRead(0) < 100){
tone(9,200,1000);
digitalWrite(red,HIGH);
}
}
Playing a sound "forever" is straightforward:
if(analogRead(A0) < 100 ) {
tone(9,2000); // once triggered, will play the sound forever
}
To switch it off, you seem to like the RESET button. So there's no need to ever call
noTone(9);
BTW: what about reading the reference ?
There is lots of ways:
Change your logic that activate the buzzer.
while (analogRead(0) < 100){
tone(9,200,1000);
}
Just use an infinite loop:
while (1) {
tone(9,200,1000);
}
Reset the Arduino to get out of the infinite loop.
An variation on this would be to replace (1) with the code that checks a pin to exit the loop or reads the sensor.
if you're really bent on using interrupts
you didn't specify what board you're working with but
for uno the 2 3 pins can be attached as interrupts and just trigger a function that turns off the tone
check out this:
attachinterrupt

How to stop pwm after generate few pulse in Arduino?

I'm trying to generate pulse from Arduino to driver motor stepper 5 phase.
The driver only needs pulse to make motor stepper work. My problem is when I'm using code like this
for(int i=0; i <= 125; i++)
{
//analogWrite(13,125);
digitalWrite(13, HIGH);
delayMicroseconds(300);
digitalWrite(13, LOW);
delayMicroseconds(300);
}
digitalWrite(13,LOW);
delay(3000);
Stepper motor can work perfectly, but after more than 10 rotation,
the angle of the motor did not return to the original place. Can we use pwm in Arduino like that? So after generating 5000 pulses using pwm, we stop the pwm?
try this code:
#include <TimerOne.h>
const byte CLOCKOUT = 11;
volatile byte counter=0;
void setup() {
Timer1.initialize(15); //Every 15 microseconds change the state of the pin in the wave function giving a period of 30 microseconds
Timer1.attachInterrupt(Onda);
pinMode (CLOCKOUT, OUTPUT);
digitalWrite(CLOCKOUT,HIGH);
}
void loop() {
if (counter>=6000){ //With 6000 changes you should achieve the amount of pulses you need
Timer1.stop(); //Here I create the dead time, which must be in HIGH
PORTB = B00001000;
counter=0;
delayMicroseconds(50);
Timer1.resume();
}
}
void Onda(){
PORTB ^= B00001000; //Change pin status
counter+=1;
}
I just cant not eliminate the jitter. If you find a solution let me know

Creating a wave using MEGA2560 (analogWrite)

Been trying to create a wave using the PWM ports (because this Arduino doesn't have DAC)of an Arduino Mega using this code. In the simulation I use a wave form generator that goes to A0, then I just want to convert it from 1023 bits to 255 but I get nothing as output.
int in = A0;
int out = 10;
void setup()
{
pinMode(in, INPUT);
pinMode(out, OUTPUT);
}
void loop(){
analogRead(in);
analogWrite(10, in/4);
}
Any suggestion would be great, thanks in advance!
You're discarding the returned value from analogRead. Change:
void loop(){
analogRead(in);
analogWrite(10, in/4);
}
to:
void loop(){
int p = analogRead(in);
analogWrite(out, p / 4);
}
The pin 10 is digital output, isn't it ?
Moreover there is a function for creating a wave : tone(pin, freq, time);

Resources