Go Down

Topic: First code (Read 4942 times) previous topic - next topic

Morke

The INPUT_PULLUP functionality is activated by writing a HIGH to the output register.
So the Arduino output will start with a HIGH level, which is the relay off state.
Oh, ok, so, the code uses INPUT_PULLUP to turn off all the relays the first time it runs, and then sets them as OUTPUT
It's like using digitalwrite(relay, HIGH) ?
What, a zoologist????

Whandall

It writes to the port like digitalWrite() does.

Code: [Select]
void pinMode(uint8_t pin, uint8_t mode)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg, *out;

if (port == NOT_A_PIN) return;

// JWS: can I let the optimizer do this?
reg = portModeRegister(port);
out = portOutputRegister(port);

if (mode == INPUT) {
uint8_t oldSREG = SREG;
                cli();
*reg &= ~bit;
*out &= ~bit;
SREG = oldSREG;
} else if (mode == INPUT_PULLUP) {
uint8_t oldSREG = SREG;
                cli();
*reg &= ~bit;
*out |= bit;
SREG = oldSREG;
} else {
uint8_t oldSREG = SREG;
                cli();
*reg |= bit;
SREG = oldSREG;
}
}

Code: [Select]
void digitalWrite(uint8_t pin, uint8_t val)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out;

if (port == NOT_A_PIN) return;

// If the pin that support PWM output, we need to turn it off
// before doing a digital write.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);

out = portOutputRegister(port);

uint8_t oldSREG = SREG;
cli();

if (val == LOW) {
*out &= ~bit;
} else {
*out |= bit;
}

SREG = oldSREG;
}
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

larryd

digitalwrite(relay, HIGH);
Seems a bit more intuitive.

.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Whandall

digitalwrite(relay, HIGH);
Seems a bit more intuitive.
Absolutely.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

dlloyd

#34
Jun 08, 2016, 02:54 am Last Edit: Jun 08, 2016, 02:54 am by dlloyd
Oh, ok, so, the code uses INPUT_PULLUP to turn off all the relays the first time it runs, and then sets them as OUTPUT
It's like using digitalwrite(relay, HIGH) ?
Simply, the two configuration methods described prevent the relay pins from temporarily going LOW prior to entering the main loop. A relay pin temporarily going LOW could cause the relay to temporarily energize during setup.

Using one of the recommended setup methods, the pin does this:

1. Most pins are initially configured as INPUT (high impedance, floating). Relay remains OFF
2. Then the internal pullup gets enabled. The pin is weakly pulled to 5V. Relay remains OFF
3. Then the pin gets configured as OUTPUT. The pin is driven to 5V (the new default). Relay remains OFF

Now all relays are under your control in the main loop.

Morke

Ok so verifying the code this error pops up
Code: [Select]

Arduino: 1.6.8 (Windows 7), Board: "Arduino/Genuino Uno"

 In function 'void setup()':

temprelayfinal2:42: error: a function-definition is not allowed here before '{' token

 {

 ^

temprelayfinal2:75: error: expected '}' at end of input

 }

 ^

exit status 1
a function-definition is not allowed here before '{' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.



And it shows a red line here:

Code: [Select]

void setup()
[color=red]{[/color]
  analogReference(INTERNAL);
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) {
    pinMode(relayPin[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF
  }
}
What, a zoologist????

Whandall

And the code above the failing definition of setup() has the error.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Morke

nvm i found the error
What, a zoologist????

Morke

Would it be possible to add an array to make the relays work on Avg temperatures to smooth the readings?
What, a zoologist????

dlloyd

#39
Jun 08, 2016, 03:48 am Last Edit: Jun 08, 2016, 04:14 am by dlloyd
Averaging might not be needed. Try adding an extra reading so there's 2 readings in a row. The first will be overwritten. The second will be more accurate because there's been a bit more time for the ADC value to settle.

Code: [Select]
reading[i] = analogRead(tempPin[i]);
reading[i] = analogRead(tempPin[i]);

EDIT: added above and some simple averaging for readings in code in reply#27.

Morke

#40
Jun 08, 2016, 03:42 pm Last Edit: Jun 08, 2016, 04:14 pm by Morke
So I'm trying to add an average to smooth things out and to practice coding.

Could you correct the code? i'm sure it's wrong, but I want to know why.
Thank you



Code: [Select]

const byte tempPin[] = {A1, A2, A3, A4};
const byte relayPin[] = {6, 7, 8, 9};

// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 24;
const byte upperLimit = 31;

float tempC[4];
word reading[4];

const int numReadings = 50; //added
int readings[numReadings];                  //added
int readIndex = 0;                              //added
int total = 0;                                     //added
int average[4] = 0;[/color]                  //addded


word printInterval = 1000; // 1 second
unsigned long printCheck = 0, lastPrintTime = 0;

void setup()
{
  analogReference(INTERNAL);
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) {
    pinMode(relayPin[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF
  }
 for (int thisReading = 0; thisReading < numReadings; thisReading++){ //added
    readings[thisReading] = 0;                                                        //added
  }
}

void loop()
{
  // readings and control
  for (int i = 0; i < 4; i++) {
    reading[i] = analogRead(tempPin[i]);
    tempC[i] = reading[i] / 9.31;
    total[i] = total[i] - readings[readIndex];  //added
    readings[readIndex] = tempC[i]                            //added
    total[i] = total[i] + readings[readIndex]                 //added
    readIndex[i] = readIndex[i] + 1 ;                          //added
    if (readIntex[i] >= numReadings) {                        //added
      readIndex = 0;                                                //added 
    }
    average[i] = total[i] / numReadings;[/color]           //added
   
    if (average[i]< lowerLimit) {
      digitalWrite(relayPin[i], LOW);   //relay OFF
    }
    else if (average[i] > upperLimit) {
      digitalWrite(relayPin[i], HIGH);  // relay ON
    }
  }
  printCheck = millis() - lastPrintTime;
  if (printCheck >= printInterval) {
    for (int i = 0; i < 4; i++) {
      Serial.print("TempC");
      Serial.print(i + 1);
      Serial.print(" ");
      Serial.println(average[i]);
    }
    Serial.println();
    lastPrintTime = millis(); // reset print timer
  }
}
What, a zoologist????

PaulS

Does your code really have [ color=red ] crap in it?

Morke

#42
Jun 08, 2016, 04:39 pm Last Edit: Jun 08, 2016, 05:05 pm by Morke
Does your code really have [ color=red ] crap in it?
Nah, I tried to highmark the changes and forgot to delete the bb code afterwards. Sorry
What, a zoologist????

Morke

Ok, a guy made it A LOT SIMPLER using Exponential smoothing:




Code: [Select]

const byte tempPin[] = {A1, A2, A3, A4};
const byte relayPin[] = {6, 7, 8, 9};

// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 24;
const byte upperLimit = 31;

const int numReadings = 64;
const word printInterval = 1000; // 1 second

float tempC[4];
unsigned long lastPrintTime = 0;

void setup()
{
  analogReference(INTERNAL);
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) {
    digitalWrite(relayPin[i], HIGH);
    pinMode(relayPin[i], OUTPUT);
  }
}

void loop()
{
  // readings and control
  for (int i = 0; i < 4; i++) {
    float raw_temp = analogRead(tempPin[i]) / 9.31;
    tempC[i] += (raw_temp - tempC[i]) / numReadings;  // filter
    if (tempC[i] < lowerLimit) {
      digitalWrite(relayPin[i], LOW);   //relay OFF
    }
    else if (tempC[i] > upperLimit) {
      digitalWrite(relayPin[i], HIGH);  // relay ON
    }
  }
  if (millis() - lastPrintTime >= printInterval) {
    for (int i = 0; i < 4; i++) {
      Serial.print("tempC");
      Serial.print(i + 1);
      Serial.print(" ");
      Serial.println(tempC[i]);
    }
    Serial.println();
    lastPrintTime = millis(); // reset print timer
  }
}
What, a zoologist????

Go Up