# percent value

``````void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:

int sum1 = 100;
int sum2 = 1023;
int sum3 = sensorValue * sum1;
int sum4 = sum3 / sum2;

Serial.println(sum4);// print out the value you read:
delay(1);        // delay in between reads for stability
}
``````

the code above is supposed to give off a percentage rather than 0-1023. the maths is right but for some reason it is shooting out negative numbers and rather than smoothly going up to 100 it shows below when i smoothly move up and down on the pot. tried 3 pots results are similiar

``````0
0
0
0
0
0
0
0
0
0
2
2
2
2
2
2
2
30
30
30
30
30
-18
-18
-18
-14
-14
-14
-14
5
5
5
5
5
31
31
31
31
31
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
-28
1
1
1
1
1
-14
-14
-14
-14
26
26
26
26
5
5
5
5
5
5
0
0
``````
``````int sum3 = sensorValue * sum1;
``````

What is the result of that calculation? Is it larger than 32767, the largest number that an int data type can hold before it rolls over? Think long.

that will be it… however am unable to figure a way around it maybe use long instead?

Yup. And if the numbers are not ever going to be negative use unsigned long. You get twice the room. Any timing variables used with millis() (or micros()) should be unsigned long.

converted all to longs... that worked ty
now how can you make a time that goes from 20 seconds to 2 hours? will long work there too

How big of a number represents 2 hours?

``````int fwdled = 9; // fwd polarity
int rvsled = 6; // reverse polarity
int relayled = 7;// relay
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(fwdled, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
long maxpercent1 = maxpercent * 100 / 1023;

long percentofmax = percofperc  * 100 / 1023 / 5;

long intervaltimer1 = intervaltimer * 1000 / 1023 * 3600;

long ontime1 = ontime + 1;
long ontime2 = intervaltimer1 / ontime1;

long interval = ontime2;

// long percentofmax = percofperc  * 100 / 1023 / 5;

// long percentofmax = percofperc  * 100 / 1023 / 5;

// Serial.print("max percent = ");
// Serial.println(maxpercent);// print out the value you read:
// delay(1);        // delay in between reads for stability

// Serial.print("percent of max = ");
// Serial.println(percentofmax);// print out the value you read:
// delay(1);        // delay in between reads for stability

// Serial.print("interval timer = ");
// Serial.println(intervaltimer1);// print out the value you read:
// delay(1);        // delay in between reads for stability

Serial.print("on time = ");
Serial.println(ontime2);// print out the value you read:
delay(1);        // delay in between reads for stability

// Serial.print("analog a4 = ");
// Serial.println(maxpercent);// print out the value you read:
// delay(1);        // delay in between reads for stability

// Serial.print("analog a5 = ");
// Serial.println(maxpercent);// print out the value you read:
// delay(1);        // delay in between reads for stability

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(fwdled, ledState);
}
}
``````

ok so most of the way there to where I want it now
a few cahnges I want to make is
LED on Pin 9 goes on for time1 (time1 can be up to 2 hours and is accumulated time of ontime)
then then off for 2 minutes
then LED on pin 7 on for 2 minutes
then off then off for 2 minutes
LED on pin 6 goes on for time1
then then off for 2 minutes
then LED on pin 7 on for 2 minutes
then off then off for 2 minutes
then cycle repeat