Am trying to learn more about ADC. Using Arduino uno as SPI to program an Attiny85. Currently having problems getting my code to work. would appreciate any help I would get.
Code:
Moderator edit: added code tags
Code:
Code:
const int soilhalfwet = 512; // Half value of the analog input total is 1023
float analogResult = 0. ; //Place holder for the data received from sensor
int sensorValue = PB4; // Input on pin 3 of attiny for adc sensor int sensorparameter = 0; // variable place holder
int Mgate = PB1; //Output on pin 6 that controls the mosfet
void setup() {
// put your setup code here, to run once:
//Serial.begin(9600);
pinMode(sensorValue, INPUT); // sets the pin as input
pinMode(Mgate, OUTPUT); // sets the pin as output
setupADC();
}
void loop() {
startConversion();
while (ADCSRA & (1<<ADSC)); // wait for conversion to complete
analogResult = (ADCH << 8)| ADCL; // save results to analogresult variable
if (analogResult >= soilhalfwet) // checks if analogresult is greater or equal to 512
{
digitalWrite(Mgate, HIGH); // Turn on
delay (1000);
digitalWrite(Mgate, LOW); // Turn off
}
else
{
digitalWrite(Mgate, LOW); // Turn off
}
}
void setupADC()
{
ADCSRA = (1 << ADEN ); // ADC Enable
ADMUX &= ~((1 << REFS1)| (1 << REFS0)); // using Vcc and GND as references
ADMUX |= (1 << ADLAR); // ADLAR set to 0
ADMUX= (1 << MUX1); // choosing PB4 channel
// ADCSRA = (1 << ADIE); //Interrupt enable
ADCSRA = (1 << ADPS2)| (1 << ADPS1)| (1 << ADPS0); //sets prescaler to 128 (8*10^6/128 = 62,5Khz)
}
void startConversion()
{
ADCSRA |= (1 << ADSC);
}