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 sensorValue = analogRead(A0);
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
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.
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 maxpercent = analogRead(A0);
long maxpercent1 = maxpercent * 100 / 1023;
long percofperc = analogRead(A1);
long percentofmax = percofperc * 100 / 1023 / 5;
long intervaltimer = analogRead(A2);
long intervaltimer1 = intervaltimer * 1000 / 1023 * 3600;
long ontime = analogRead(A3);
long ontime1 = ontime + 1;
long ontime2 = intervaltimer1 / ontime1;
long interval = ontime2;
// long percofperc = analogRead(A4);
// long percentofmax = percofperc * 100 / 1023 / 5;
// long percofperc = analogRead(A5);
// 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