Sleep/Wake function

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
asmith1265
 
Posts: 1
Joined: Mon Dec 01, 2014 12:31 pm

Sleep/Wake function

Post by asmith1265 »

I need to code a DC motor to run at a certain speed according to the light. The motor must stop if there is no light. Also I want to set the robot to sleep after 60 seconds and be awoken by a switch. Here is the code I have as of right now the motor isn't even running. I'm sure my wiring is set up right, can anybody please help?
#include <avr/power.h>
#include <avr/sleep.h>
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
int lightPin = 0;
int baselevel;
int lightlevel;
int sleepStatus = 0;
int count = 0;
 


void setup()
{
  baselevel = analogRead(lightPin);
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(switchPin, INPUT);
  Serial.begin(9600);
}
 

void switchPinInterrupt (void)
{detachInterrupt(0);
}


void sleepNow(void)
{
attachInterrupt (0,switchPinInterrupt,LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}

void loop()
{
  
  
  count++;
  delay(1000);
  lightlevel = analogRead(lightPin);
  
  if ((lightlevel + 100) < baselevel)
  {int speed = 0;
  }
  else
  {  int speed = analogRead(lightPin) / 4;}
if (count >= 60)
{count = 0;
delay(100);
sleepNow();
}

}
 

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Sleep/Wake function

Post by adafruit_support_bill »

the motor isn't even running
What are you using to drive the motor? I don't see any code for that.

Code: Select all

  {int speed = 0;
  }
  else
  {  int speed = analogRead(lightPin) / 4;}
The scope of your speed variables is limited to the blocks in which they are declared. They don't exist outside that scope and are not used for anything inside that scope.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”