loading

Arduino code help

Hey guys first post.  I need some help with some code Im working on. What I want to do is have a pot controlled timer that moves a servo after a time dictated by the pot. The scale of delays I want is 0-3 minutes.  I eventually want to program an Attiny85 chip to do this. For now Im substituting a led in for the servo. Im a newbie with Arduino so any help with this simple project would be greatly appreciated. This is my code: 

int sensorPin = 0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
    pinMode(ledPin, OUTPUT);
   int val = analogRead(0);
    val = map(val, 0, 1023, 0, 30000);

      digitalWrite(ledPin, HIGH); 
  
  delay(val);

delay(val);

delay(val);

delay(val);

delay(val);

delay(val);

       
  digitalWrite(ledPin, LOW);  
 
                   
}
void loop() {
                 
}




i'm using mine to create an optical tachometer using an IR emitter and detector from Radioshack and I don't know how to fix my many errors. I'm very new to Arduino. Can you help me please?

#include <LiquidCrystal.h>

LiquidCrystal 8, 10, 9; // DEFINE LCD PINS

volatile byte REV; // VOLATILE DATA TYPE TO STORE REVOLUTIONS

unsigned long int rpm, maxRPM; // DEFINE RPM AND MAXIMUM RPM

unsigned long time; // DEFINE TIME TAKEN TO COVER ONE REVOLUTION

int ledPin = 12; // STATUS LED

int led = 0,RPMlen , prevRPM; // INTEGERS TO STORE LED VALUE AND CURRENT RPM AND PREVIOUS RPM

int flag = 0; // A VARIABLE TO DETERMINE WHETHER THE LCD NEEDS TO BE CLEARED OR NOT

long prevtime = 0; // STORE IDLE TIME TO TOGGLE MENU

void setup()
{
Serial.begin 9600 // GET VALUES USING SERIAL MONITOR

LiquidCrystal.begin 16, 2; // INITIATE LCD

attachInterrupt(0, RPMCount, RISING); // ADD A HIGH PRIORITY ACTION ( AN INTERRUPT) WHEN THE SENSOR GOES FROM LOW TO HIGH

REV = 0; // START ALL THE VARIABLES FROM 0

rpm = 0;

time = 0;

pinMode ledPin, OUTPUT;

pinMode 3, OUTPUT;

pinMode 4, OUTPUT;

digitalWrite(3, HIGH); // VCC PIN FOR SENSOR

digitalWrite(4, LOW); // GND PIN FOR SENSOR

LiquidCrystal.print("TACHOMETER"); // STARTUP TEXT

LiquidCrystal.setCursor(0, 1);

LiquidCrystal.print("- Adam Gipe"); // THAT'S ME

delay(2000);

LiquidCrystal.clear();

}

void loop()
{
long currtime = millis(); // GET CURRENT TIME

long idletime = currtime - prevtime; // CALCULATE IDLE TIME

if(REV >= 5 ) // IT WILL UPDATE AFETR EVERY 5 READINGS
{


if(flag==0) // CLEAR THE LCD TO AVOID ANY GARBAGE TEXT
{
LiquidCrystal.clear();

LiquidCrystal.print("SENSOR MEASURING");

flag=1; // AFTER FLAG = 1 , THE LOOP WILL NOT EXECUTE AGAIN
}

rpm = 30*1000/(millis() - time)*REV; // CALCULATE RPM USING REVOLUTIONS AND ELAPSED TIME

if(rpm > maxRPM)

maxRPM = rpm; // GET THE MAX RPM THROUGHOUT THE RUN

time = millis();

REV = 0;

int x= rpm;
// CALCULATE NUMBER OF DIGITS IN RPM
while(x!=0)
{
x = x/10;

RPMlen++;
}



if(RPMlen!=prevRPM) // IF THE RPM FALLS TO A LOWER NUMBER WITH LESS DIGITS , THE LCD WILL GET CLEARED
{
LiquidCrystal.clear();

prevRPM = RPMlen;

flag=0;

LiquidCrystal.print("SENSOR MEASURING");
}

LiquidCrystal.setCursor(0, 1);

LiquidCrystal.print(rpm,DEC); // PRINT RPM IN DECIMAL SYSTEM

LiquidCrystal.setCursor(6,1);

LiquidCrystal.print("RPM");

delay(500);

prevtime = currtime; // RESET IDLETIME

}

if(idletime > 5000 ) // IF THERE ARE NO READING FOR 5 SEC , THE SCREEN WILL SHOW MAX RPM
{

if(flag==1) // CLEAR THE LCD
{
LiquidCrystal.clear();
flag=0;
}

LiquidCrystal.clear();

LiquidCrystal.print("MAXIMUM RPM");

LiquidCrystal.setCursor(0, 1);

LiquidCrystal.print(maxRPM,DEC); // DISPLAY MAX RPM

LiquidCrystal.print(" RPM");

delay(2000);

LiquidCrystal.clear();

LiquidCrystal.print("IDLE STATE");

LiquidCrystal.setCursor(0, 1);

LiquidCrystal.print("READY TO MEASURE");

delay(2000);

prevtime = currtime;
}

}

void RPMCount() // EVERYTIME WHEN THE SENSOR GOES FROM LOW TO HIGH , THIS FUNCTION WILL BE INVOKED
{
REV++; // INCREASE REVOLUTIONS

if (led == LOW)
{

led = HIGH; // TOGGLE STATUS LED
}

else
{
led = LOW;
}
digitalWrite(ledPin, led);
}

error codes.JPG
Steven Wrigley (author) 4 years ago
Thank you very much sir! That works fantastically!

I though you might be intrested in the ATTiny ISP Shield

https://www.indiegogo.com/projects/attiny-isp-shield

I don't profess to know anything about the Arduino, I only got one yesterday but the following code probably does what you want. I wasn't sure on what you wanted to do with the servo, IE move it fully after the desired time or just increase it one step so I just operated an LED as you did.

The value of the pot is read at startup and the code just cycles through indefinitely so the time value of the pot can only be changed by resetting the Arduino. It's easy enough to change though if you need to.

I'm guessing there may be other ways using the milli function but as this seems to reset every x amount of days this seemed the most straight forward. 

If your code becomes complicated, you may have to reduce the 10ms timer and increase the count. I'm not sure on how fast Arduinos run yet.


/*
    Arduino Uno sketch
   
    Reads the value of a pot on A0 at start up and converts the value
    to a time between 0 & 3 mins.
   
    Toggles a LED on and off at the measured time interval
   
*/



int sensorPin = A0;    // select the input pin for the pot
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // Analogue Value
int secondsCounter = 0;     // Holds the number of seconds gone by
int milliSecondsCounter = 0; // Holds a count of how many milliseconds gone by
int analogValueSecs = 0; // Stores the value of the pot mapped to seconds
byte ledDirection = HIGH; // Stores the state of the LED

void setup()
{
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT); 
 
  // Turn the LED off
  digitalWrite (ledPin, LOW);
 
  // Read the analogue value
sensorValue = analogRead(sensorPin);
 
  //convert the analogue value to a max of 180 secs - 3 mins
analogValueSecs = map (sensorValue, 0, 1023, 0, 180);
 
}

void loop()
{
delay (10); // delay 10ms
milliSecondsCounter += 10; // add 10 ms to the counter

// if a 1000 milliseconds has passed, add a second on to the seconds counter
// and reset the milliseconds counter for the loop
if (milliSecondsCounter >= 1000)
{
     secondsCounter++ ;
     milliSecondsCounter = 0;
}

// if the seconds counter has reached the required time turn the LED on
// invert the on / off direction of the LED and reset the seconds counter
// for the loop
if (secondsCounter >= analogValueSecs)
    {
       digitalWrite (ledPin, ledDirection);
       ledDirection = !ledDirection;
       secondsCounter = 0;
    }
}